update
goes via the ActiveRecord record, while update_all
writes to the column in SQL.
update
will do Rails magic, such as allowing a model to be passed to it:
Post.find_each {|post| post.update(category: nil)}
Rails runs:
UPDATE posts SET category_id = NULL WHERE posts.id = #{post.id}
But with:
Post.update_all(category: nil)
Rails tries to run:
UPDATE posts SET category = NULL
Which errors, because category
isn’t a column.
.update_all
should be used very sparingly, but where it is used (e.g. in a data migration), you need to refer to table columns, not record attributes.
— Adam Dawkins
Who are Dragon Drop?
Dragon Drop is a specialist web and app development agency. The team has extensive full-stack technical credentials and a strong focus on user experience.
The Dragon Drop founding team have over 40 years of web development and project experience. They have managed or developed over 100 significant ecommerce and web projects during their careers, including implementations for major UK high street retailers, financial services companies and government agencies.
Their approach to innovative solutions stems from perspectives gained as retailer, software supplier and web agency.