Created
May 8, 2012 15:42
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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