instance method
attributes=
Ruby on Rails 3.1.12
Since v2.2.3 Last seen in v3.1.12Signature
attributes=(new_attributes, guard_protected_attributes = nil)
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.
The guard_protected_attributes argument is now deprecated, use the assign_attributes method if you want to bypass mass-assignment security.
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
Parameters
-
new_attributesreq -
guard_protected_attributesopt = nil
Source
# File activerecord/lib/active_record/base.rb, line 1704
def attributes=(new_attributes, guard_protected_attributes = nil)
unless guard_protected_attributes.nil?
message = "the use of 'guard_protected_attributes' will be removed from the next minor release of rails, " +
"if you want to bypass mass-assignment security then look into using assign_attributes"
ActiveSupport::Deprecation.warn(message)
end
return unless new_attributes.is_a?(Hash)
if guard_protected_attributes == false
assign_attributes(new_attributes, :without_protection => true)
else
assign_attributes(new_attributes)
end
end
Defined in activerecord/lib/active_record/base.rb line 1704
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Base