Skip to content

Instantly share code, notes, and snippets.

@brutuscat
Created May 8, 2012 15:42
Show Gist options
  • Save brutuscat/2636478 to your computer and use it in GitHub Desktop.
Save brutuscat/2636478 to your computer and use it in GitHub Desktop.
A Weak Attributes-Assignment Method for ActiveRecords, that merges in the values only when the attribute is blank?
def weak_assign_attributes(new_attributes)
new_attributes = new_attributes.stringify_keys
new_attributes = self.attributes.merge(new_attributes){|key, old_val, new_val| old_val.present? ? old_val : new_val}.
diff(self.attributes)
self.attributes = new_attributes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment