assign_attributes
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-30)
- 4.1.8 (0)
- 4.2.1 (21)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
assign_attributes(new_attributes, options = {})
public
Allows you to set all the attributes for a particular mass-assignment security role by passing in a hash of attributes with keys matching the attribute names (which again matches the column names) and the role name using the :as option.
To bypass mass-assignment security you can use the :without_protection => true option.
class User < ActiveRecord::Base attr_accessible :name attr_accessible :name, :is_admin, :as => :admin end user = User.new user.assign_attributes({ :name => 'Josh', :is_admin => true }) user.name # => "Josh" user.is_admin? # => false user = User.new user.assign_attributes({ :name => 'Josh', :is_admin => true }, :as => :admin) user.name # => "Josh" user.is_admin? # => true user = User.new user.assign_attributes({ :name => 'Josh', :is_admin => true }, :without_protection => true) user.name # => "Josh" user.is_admin? # => true
ActiveModel::MassAssignmentSecurity::Error
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.