Methods
Included Modules
- Rails START:includes
Classes and Modules
Instance Public methods
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
# File activerecord/lib/active_record/attribute_assignment.rb, line 66 66: def assign_attributes(new_attributes, options = {}) 67: return unless new_attributes 68: 69: attributes = new_attributes.stringify_keys 70: multi_parameter_attributes = [] 71: nested_parameter_attributes = [] 72: @mass_assignment_options = options 73: 74: unless options[:without_protection] 75: attributes = sanitize_for_mass_assignment(attributes, mass_assignment_role) 76: end 77: 78: attributes.each do |k, v| 79: if k.include?("(") 80: multi_parameter_attributes << [ k, v ] 81: elsif respond_to?("#{k}=") 82: if v.is_a?(Hash) 83: nested_parameter_attributes << [ k, v ] 84: else 85: send("#{k}=", v) 86: end 87: else 88: raise(UnknownAttributeError, "unknown attribute: #{k}") 89: end 90: end 91: 92: # assign any deferred nested attributes after the base attributes have been set 93: nested_parameter_attributes.each do |k,v| 94: send("#{k}=", v) 95: end 96: 97: @mass_assignment_options = nil 98: assign_multiparameter_attributes(multi_parameter_attributes) 99: end
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
Instance Protected methods