No possibility to update model attributes with a block syntax?

activerecord, ruby-on-rails, ruby-on-rails-3, ruby-on-rails-4

Solution

Use, #tap (available since Ruby 1.9) if you want the block syntax:

user.tap do |u|
  u.mail = "dhh@rubyonrails.com"
end
user.save

One caveat in Rails 3 is it avoids mass assignment protection compared to `#update_attributes`, which may or may not be what you want. In Rails 4 of course it doesn't matter because attributes are protected differently (using strong parameters).

Problem

There is a block syntax for new and create that goes like this: ``` user = User.create do |u| u.name = "David" u.mail = "dhh@rails.com" end ``` Is there a block syntax that would be valid in Rails 3 and Rails 4 for updating attributes? Something like: ``` user = User.where(name: "David").first user.update_attributes do |u| u.mail = "dhh@rubyonrails.com" end ``` Maybe not `update_attributes` but something similar. I have been searching the web and the Rails 4 source on Github, and i think there isn't such a thing. Am I wrong? P.S. i'm not looking for making any monkey patch methods or something similar, just interested if there is a method that comes by default with `ActiveRecord`.

Original source