Change multiple object attributes in a single line

The methods you're looking for are called assign_attributes or update_attributes.

@object.assign_attributes(:attr3 => "asdfasdf", :attr4 => "asdfasf") # @object now has attr3 and attr4 set.
@object.update_attributes(:attr3 => "asdfasdf", :attr4 => "asdfasf") # @object now has attr3 and attr4 set and committed to the database.

There are some security concerns with using these methods in Rails applications. If you're going to use either one, especially in a controller, be sure to check out the documentation on attr_accessible so that malicious users can't pass arbitrary information into model fields you would prefer didn't become mass assignable.


You want to use assign_attributes or update (which is an alias for the deprecated update_attributes):

@object.assign_attributes(:attr3 => "asdfasdf", :attr4 => "asdfasdf")

@object.update(attr3: "asdfasdf", attr4: "asdfasdf")

If you choose to update instead, the object will be saved immediately (pending any validations, of course).