跳至内容 跳至搜索
方法
A
D
H
I

实例公共方法

alias_attribute(new_name, old_name)

允许你为属性创建别名。

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

person = Person.create(name: 'Bob')
person.name     # => "Bob"
person.nickname # => "Bob"

别名也可以用于查询

Person.where(nickname: "Bob")
# SELECT "people".* FROM "people" WHERE "people"."name" = "Bob"
# File activerecord/lib/active_record/attribute_methods.rb, line 66
def alias_attribute(new_name, old_name)
  super

  if @alias_attributes_mass_generated
    ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |code_generator|
      generate_alias_attribute_methods(code_generator, new_name, old_name)
    end
  end
end

attribute_method?(attribute)

如果 attribute 是一个属性方法且表存在,则返回 true,否则返回 false

class Person < ActiveRecord::Base
end

Person.attribute_method?('name')   # => true
Person.attribute_method?(:age=)    # => true
Person.attribute_method?(:nothing) # => false
# File activerecord/lib/active_record/attribute_methods.rb, line 224
def attribute_method?(attribute)
  super || (table_exists? && column_names.include?(attribute.to_s.delete_suffix("=")))
end

attribute_names()

如果不是抽象类且表存在,则返回字符串形式的列名数组。否则返回空数组。

class Person < ActiveRecord::Base
end

Person.attribute_names
# => ["id", "created_at", "updated_at", "name", "age"]
# File activerecord/lib/active_record/attribute_methods.rb, line 236
def attribute_names
  @attribute_names ||= if !abstract_class? && table_exists?
    attribute_types.keys
  else
    []
  end.freeze
end

dangerous_class_method?(method_name)

一个类方法如果已经被 Active Record (重新) 定义,但不是由任何祖先定义,则它是“危险的”。(所以 'puts' 不危险,但 'new' 危险。)

# File activerecord/lib/active_record/attribute_methods.rb, line 201
def dangerous_class_method?(method_name)
  return true if RESTRICTED_CLASS_METHODS.include?(method_name.to_s)

  if Base.respond_to?(method_name, true)
    if Object.respond_to?(method_name, true)
      Base.method(method_name).owner != Object.method(method_name).owner
    else
      true
    end
  else
    false
  end
end

has_attribute?(attr_name)

如果给定的属性存在,则返回 true,否则返回 false。

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

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 254
def has_attribute?(attr_name)
  attr_name = attr_name.to_s
  attr_name = attribute_aliases[attr_name] || attr_name
  attribute_types.key?(attr_name)
end

instance_method_already_implemented?(method_name)

当 Active Record 方法在模型中定义时,会引发一个 ActiveRecord::DangerousAttributeError 异常,否则返回 false

class Person < ActiveRecord::Base
  def save
    'already defined by Active Record'
  end
end

Person.instance_method_already_implemented?(:save)
# => ActiveRecord::DangerousAttributeError: save is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name.

Person.instance_method_already_implemented?(:name)
# => false
# File activerecord/lib/active_record/attribute_methods.rb, line 165
def instance_method_already_implemented?(method_name)
  if dangerous_attribute_method?(method_name)
    raise DangerousAttributeError, "#{method_name} is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name."
  end

  if superclass == Base
    super
  else
    # If ThisClass < ... < SomeSuperClass < ... < Base and SomeSuperClass
    # defines its own attribute method, then we don't want to override that.
    defined = method_defined_within?(method_name, superclass, Base) &&
      ! superclass.instance_method(method_name).owner.is_a?(GeneratedAttributeMethods)
    defined || super
  end
end