Method: Mongoid::Attributes#assign_attributes

Defined in:
lib/mongoid/attributes.rb

#assign_attributes(attrs = nil, options = {}) ⇒ Object

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.

Examples:

Assign the attributes.

person.assign_attributes(:title => "Mr.")

Assign the attributes (with a role).

person.assign_attributes({ :title => "Mr." }, :as => :admin)

Parameters:

  • attrs (Hash) (defaults to: nil)

    The new attributes to set.

  • options (Hash) (defaults to: {})

    Supported options: :without_protection, :as

Since:

  • 2.2.1



131
132
133
134
135
136
137
# File 'lib/mongoid/attributes.rb', line 131

def assign_attributes(attrs = nil, options = {})
  _assigning do
    process(attrs, options[:as] || :default, !options[:without_protection]) do |document|
      document.identify if new? && id.blank?
    end
  end
end