instance method
attributes=
Ruby on Rails 2.3.18
Since v2.2.3 Last seen in v3.1.12Signature
attributes=(new_attributes, guard_protected_attributes = true)
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 guard_protected_attributes is true (the default), then sensitive attributes can be protected from this form of mass-assignment by using the attr_protected macro. Or you can alternatively specify which attributes can be accessed with the attr_accessible macro. Then all the attributes not included in that won’t be allowed to be mass-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 user.send(:attributes=, { :username => 'Phusion', :is_admin => true }, false) user.is_admin? # => true
Parameters
-
new_attributesreq -
guard_protected_attributesopt = true
Source
# File activerecord/lib/active_record/base.rb, line 2789
def attributes=(new_attributes, guard_protected_attributes = true)
return if new_attributes.nil?
attributes = new_attributes.dup
attributes.stringify_keys!
attributes = remove_attributes_protected_from_mass_assignment(attributes) if guard_protected_attributes
assign_attributes(attributes) if attributes and attributes.any?
end
Defined in activerecord/lib/active_record/base.rb line 2789
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Base