[Feature][ActiveRecord] Add a method to assign attributes without changes

16 views
Skip to first unread message

Khiav REOY

unread,
Apr 23, 2018, 10:11:19 PM4/23/18
to Ruby on Rails: Core

After using update_all to update a bunch of records in one query, I would like to refresh one model object of them. I avoid using reload method, since it doesn't have to load the data from database. I can assign the new attributes by using assign_attributes method. But in this way the model object is changed. For example:


User.where('').update_all(column => value)

current_user.assign_attributes(column => value)
current_user.changed?
# => true


I find a workaround by calling clear_attribute_changes method


current_user.assign_attributes(column => value)
current_user.send(:clear_attribute_changes, column)
current_user.changed?
# => false


It's ugly, I think it will be cleaner if we have a assign_attributes_without_changes method.


current_user.assign_attributes_without_changes(column => value)
current_user.changed?
# => false

Reply all
Reply to author
Forward
0 new messages