Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # response to http://www.davidverhasselt.com/set-attributes-in-activerecord/#comment-1663663372
- # just remember that this code requires you to either use super or attr_name_will_change! in your overridden setters and getters
- # @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
- def update_attribute_without_other_attributes(name, value)
- # get all changed values (self.changes returns hash with [ old_value, new_value ] or nil as values, we only want new_value)
- changes_before = self.changes.inject({}) { |h, (k,v)| h[k] = (v.respond_to?(:last) ? v.last : v); h }
- changes_before.delete name
- restore_attributes # reset_changes for Rails 4.1
- save_result = update_attribute(name, value)
- # restore changes
- attributes(changes_before)
- save_result
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement