跳至内容 跳至搜索

Active Record 属性方法

命名空间
方法
#
A
H
R
包含的模块

常量

RESTRICTED_CLASS_METHODS = %w(private public protected allocate new name superclass)
 

实例公共方法

[](attr_name)

返回经过类型转换后的由 attr_name 标识的属性的值。(有关特定类型转换行为的信息,请参阅 ActiveModel::Type 下的类型。)

class Person < ActiveRecord::Base
  belongs_to :organization
end

person = Person.new(name: "Francesco", date_of_birth: "2004-12-12")
person[:name]            # => "Francesco"
person[:date_of_birth]   # => Date.new(2004, 12, 12)
person[:organization_id] # => nil

如果属性丢失,则引发 ActiveModel::MissingAttributeError。但是请注意,id 属性永远不会被视为丢失。

person = Person.select(:name).first
person[:name]            # => "Francesco"
person[:date_of_birth]   # => ActiveModel::MissingAttributeError: missing attribute 'date_of_birth' for Person
person[:organization_id] # => ActiveModel::MissingAttributeError: missing attribute 'organization_id' for Person
person[:id]              # => nil
# File activerecord/lib/active_record/attribute_methods.rb, line 415
def [](attr_name)
  read_attribute(attr_name) { |n| missing_attribute(n, caller) }
end

[]=(attr_name, value)

使用指定的 value 更新由 attr_name 标识的属性。属性值将在读取时进行类型转换。

class Person < ActiveRecord::Base
end

person = Person.new
person[:date_of_birth] = "2004-12-12"
person[:date_of_birth] # => Date.new(2004, 12, 12)
# File activerecord/lib/active_record/attribute_methods.rb, line 428
def []=(attr_name, value)
  write_attribute(attr_name, value)
end

accessed_fields()

返回从该模型读取过的所有数据库字段的名称。这在开发模式下可能很有用,用于确定需要选择哪些字段。对于性能关键页面,仅选择所需字段可以轻松提升性能(假设您使用了模型的所有字段)。

例如

class PostsController < ActionController::Base
  after_action :print_accessed_fields, only: :index

  def index
    @posts = Post.all
  end

  private
    def print_accessed_fields
      p @posts.first.accessed_fields
    end
end

这允许您快速更改代码为

class PostsController < ActionController::Base
  def index
    @posts = Post.select(:id, :title, :author_id, :updated_at)
  end
end
# File activerecord/lib/active_record/attribute_methods.rb, line 460
def accessed_fields
  @attributes.accessed
end

attribute_for_inspect(attr_name)

返回属性 attr_name 值的类似 inspect 的字符串。String 属性最多截断为 50 个字符。其他属性将按原样返回 inspect 的值。

person = Person.create!(name: 'David Heinemeier Hansson ' * 3)

person.attribute_for_inspect(:name)
# => "\"David Heinemeier Hansson David Heinemeier Hansson ...\""

person.attribute_for_inspect(:created_at)
# => "\"2012-10-22 00:15:07.000000000 +0000\""

person.attribute_for_inspect(:tag_ids)
# => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]"
# File activerecord/lib/active_record/attribute_methods.rb, line 365
def attribute_for_inspect(attr_name)
  attr_name = attr_name.to_s
  attr_name = self.class.attribute_aliases[attr_name] || attr_name
  value = _read_attribute(attr_name)
  format_for_inspect(attr_name, value)
end

attribute_names()

返回此对象上可用属性名称的数组。

class Person < ActiveRecord::Base
end

person = Person.new
person.attribute_names
# => ["id", "created_at", "updated_at", "name", "age"]
# File activerecord/lib/active_record/attribute_methods.rb, line 334
def attribute_names
  @attributes.keys
end

attribute_present?(attr_name)

如果指定 attribute 已被用户或数据库加载设置,并且既不是 nil 也不是 empty?(后者仅适用于响应 empty? 的对象,最明显的是字符串),则返回 true。否则,返回 false。请注意,对于布尔属性,它始终返回 true

class Task < ActiveRecord::Base
end

task = Task.new(title: '', is_done: false)
task.attribute_present?(:title)   # => false
task.attribute_present?(:is_done) # => true
task.title = 'Buy milk'
task.is_done = true
task.attribute_present?(:title)   # => true
task.attribute_present?(:is_done) # => true
# File activerecord/lib/active_record/attribute_methods.rb, line 387
def attribute_present?(attr_name)
  attr_name = attr_name.to_s
  attr_name = self.class.attribute_aliases[attr_name] || attr_name
  value = _read_attribute(attr_name)
  !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
end

attributes()

返回所有属性的哈希,其中键是属性名称,值是属性的值。

class Person < ActiveRecord::Base
end

person = Person.create(name: 'Francesco', age: 22)
person.attributes
# => {"id"=>3, "created_at"=>Sun, 21 Oct 2012 04:53:04, "updated_at"=>Sun, 21 Oct 2012 04:53:04, "name"=>"Francesco", "age"=>22}
# File activerecord/lib/active_record/attribute_methods.rb, line 346
def attributes
  @attributes.to_hash
end

has_attribute?(attr_name)

如果给定属性存在于 attributes 哈希中,则返回 true,否则返回 false

class Person < ActiveRecord::Base
  alias_attribute :new_name, :name
end

person = Person.new
person.has_attribute?(:name)     # => true
person.has_attribute?(:new_name) # => true
person.has_attribute?('age')     # => true
person.has_attribute?(:nothing)  # => false
# File activerecord/lib/active_record/attribute_methods.rb, line 316
def has_attribute?(attr_name)
  attr_name = attr_name.to_s
  attr_name = self.class.attribute_aliases[attr_name] || attr_name
  @attributes.key?(attr_name)
end

respond_to?(name, include_private = false)

一个带有 name 属性的 Person 对象可以询问 person.respond_to?(:name)person.respond_to?(:name=)person.respond_to?(:name?),这些都将返回 true。它还定义了属性方法(如果尚未生成)。

class Person < ActiveRecord::Base
end

person = Person.new
person.respond_to?(:name)    # => true
person.respond_to?(:name=)   # => true
person.respond_to?(:name?)   # => true
person.respond_to?('age')    # => true
person.respond_to?('age=')   # => true
person.respond_to?('age?')   # => true
person.respond_to?(:nothing) # => false
# File activerecord/lib/active_record/attribute_methods.rb, line 291
def respond_to?(name, include_private = false)
  return false unless super

  # If the result is true then check for the select case.
  # For queries selecting a subset of columns, return false for unselected columns.
  if @attributes
    if name = self.class.symbol_column_to_string(name.to_sym)
      return _has_attribute?(name)
    end
  end

  true
end