instance method attributes=

Ruby on Rails 3.1.12

Since v2.2.3 Last seen in v3.1.12

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12

Signature

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_attributes req
guard_protected_attributes opt = 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

Type at least 2 characters to search.

↑↓ navigate · open · esc close