Module: ActiveRecord::AttributeAssignment
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::MassAssignmentSecurity
- Included in:
- Base
- Defined in:
- activerecord/lib/active_record/attribute_assignment.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#assign_attributes(new_attributes, 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.
-
#attributes=(new_attributes) ⇒ Object
Allows you to set all the attributes at once by passing in a hash with keys matching the attribute names (which again matches the column names).
Methods included from ActiveSupport::Concern
append_features, extended, included
Instance Method Details
#assign_attributes(new_attributes, 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.
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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'activerecord/lib/active_record/attribute_assignment.rb', line 66 def assign_attributes(new_attributes, = {}) return if new_attributes.blank? attributes = new_attributes.stringify_keys multi_parameter_attributes = [] nested_parameter_attributes = [] @mass_assignment_options = unless [:without_protection] attributes = sanitize_for_mass_assignment(attributes, mass_assignment_role) end attributes.each do |k, v| if k.include?("(") multi_parameter_attributes << [ k, v ] elsif respond_to?("#{k}=") if v.is_a?(Hash) nested_parameter_attributes << [ k, v ] else send("#{k}=", v) end else raise(UnknownAttributeError, "unknown attribute: #{k}") end end # assign any deferred nested attributes after the base attributes have been set nested_parameter_attributes.each do |k,v| send("#{k}=", v) end @mass_assignment_options = nil assign_multiparameter_attributes(multi_parameter_attributes) end |
#attributes=(new_attributes) ⇒ Object
Allows you to set all the attributes at once by passing in a hash with keys matching the attribute names (which again matches the column names).
If any attributes are protected by either attr_protected
or attr_accessible
then only settable attributes will be assigned.
class User < ActiveRecord::Base
attr_protected :is_admin
end
user = User.new
user.attributes = { :username => 'Phusion', :is_admin => true }
user.username # => "Phusion"
user.is_admin? # => false
33 34 35 36 37 |
# File 'activerecord/lib/active_record/attribute_assignment.rb', line 33 def attributes=(new_attributes) return unless new_attributes.is_a?(Hash) assign_attributes(new_attributes) end |