Ruby on Rails latest stable (v4.2.1) - 1 note - Class: AttributeAssignment
assign_attributes(new_attributes) public

Allows you to set all the attributes by passing in a hash of attributes with keys matching the attribute names (which again matches the column names).

If the passed hash responds to permitted? method and the return value of this method is false an ActiveModel::ForbiddenAttributesError exception is raised.

cat = Cat.new(name: "Gorby", status: "yawning")
cat.attributes # =>  { "name" => "Gorby", "status" => "yawning", "created_at" => nil, "updated_at" => nil}
cat.assign_attributes(status: "sleeping")
cat.attributes # =>  { "name" => "Gorby", "status" => "sleeping", "created_at" => nil, "updated_at" => nil }

New attributes will be persisted in the database when the object is saved.

Aliased to attributes=.

Show source
Register or log in to add new notes.
September 5, 2012 - (v3.2.1 - v3.2.8)
1 thank


Note that in the example shown in the documentation, `user.assign_attributes({ :name => ‘Josh’, :is_admin => true })` would raise a `ActiveModel::MassAssignmentSecurity::Error` and would not actually update user.name, contrary to what the example seems to demonstrate.