activerecord find and update

activerecord, ruby

Solution

There is an update method in active record, which finds by id and updates attributes. But this will make two sql queries.

update(id, attributes)

eg

 Post.update(params[:id], {:title => 'Hello world'})

If you dont want validations to happen, you could use

Post.update_all({:title => 'hello1'}, {:id => params[:id]})

This will make only one sql query

Problem

Is there a method find_and_update method on ActiveRecord::Base that will basically find a record by it's id and update a given key? This will avoid having to query and then update resulting in 2 queries instead of 1. Thanks

Original source