Understanding Simple ActiveRecord Updates in Rails

I just dive into ActiveRecord and cannot find the answer to my question. If I update the attributes of an object and then call save () ... will ActiveRecord only be saved in the database when the new values ​​differ from the old values?

Say I'm doing something like this:

thing_to_update = Thing.find_or_create_by_code(some_code) if thing_to_update.name != some_name thing_to_update.update_attribute(:name, some_name) end 

I do not want to make additional calls in db if I do not need it, because I will probably have to update many objects. I tried to read the docs and it says nothing about comparing new values ​​with old ones. Did I miss something?

thanks

+6
ruby ruby-on-rails activerecord
source share
2 answers

The active record was not used for partial SQL updates, but since April 2008.

+6
source share

ActiveRecord will not update your record if the attributes have not changed. You can verify this yourself by calling thing_to_update.save from the console and observing the log. ActiveRecord will load the record, but will not try to update it.

+4
source share

All Articles