- A
- B
- C
- D
- N
- P
- S
属性
[RW] | abstract_class | 如果这是抽象类(请参阅 考虑以下默认行为
但是,当使用
请注意,在上述示例中,要禁止创建普通的 |
[R] | base_class | 返回从抽象类或从 考虑以下行为
|
实例公共方法
abstract_class?() 链接
返回此类是否是抽象类。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 167 def abstract_class? defined?(@abstract_class) && @abstract_class == true end
base_class?() 链接
返回该类是否是基类。有关详细信息,请参阅 base_class
。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 119 def base_class? base_class == self end
descends_from_active_record?() 链接
如果不需要 STI 类型条件,则返回 true
。如果需要应用 STI 类型条件,则返回 false
。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 82 def descends_from_active_record? if self == Base false elsif superclass.abstract_class? superclass.descends_from_active_record? else superclass == Base || !columns_hash.include?(inheritance_column) end end
new(attributes = nil, &block) 链接
确定传入的属性之一是否是继承列,如果继承列是 attr accessible,则它会初始化给定子类的实例,而不是基类。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 56 def new(attributes = nil, &block) if abstract_class? || self == Base raise NotImplementedError, "#{self} is an abstract class and cannot be instantiated." end if _has_attribute?(inheritance_column) subclass = subclass_from_attributes(attributes) if subclass.nil? && scope_attributes = current_scope&.scope_for_create subclass = subclass_from_attributes(scope_attributes) end if subclass.nil? && base_class? subclass = subclass_from_attributes(column_defaults) end end if subclass && subclass != self subclass.new(attributes, &block) else super end end
polymorphic_class_for(name) 链接
返回提供 name
的类。
用于查找与存储在多态类型列中的值对应的类。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 216 def polymorphic_class_for(name) if store_full_class_name name.constantize else compute_type(name) end end
polymorphic_name() 链接
返回要存储在多态 Associations
的多态类型列中的值。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 209 def polymorphic_name store_full_class_name ? base_class.name : base_class.name.demodulize end
primary_abstract_class() 链接
设置 Active Record 的应用程序记录类
如果你的应用程序使用与 ApplicationRecord 不同的类作为你的主抽象类,这很有用。此类将与 Active Record 共享数据库连接。它是连接到你的主数据库的类。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 177 def primary_abstract_class if ActiveRecord.application_record_class && ActiveRecord.application_record_class.name != name raise ArgumentError, "The `primary_abstract_class` is already set to #{ActiveRecord.application_record_class.inspect}. There can only be one `primary_abstract_class` in an application." end self.abstract_class = true ActiveRecord.application_record_class = self end
sti_class_for(type_name) 链接
返回提供 type_name
的类。
用于查找与存储在继承列中的值对应的类。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 194 def sti_class_for(type_name) if store_full_sti_class && store_full_class_name type_name.constantize else compute_type(type_name) end rescue NameError raise SubclassNotFound, "The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " \ "This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " \ "Please rename this column if you didn't intend it to be used for storing the inheritance class " \ "or overwrite #{name}.inheritance_column to use another column for that information." end
sti_name() 链接
返回要存储在 STI 的继承列中的值。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 187 def sti_name store_full_sti_class && store_full_class_name ? name : name.demodulize end
实例受保护的方法
compute_type(type_name) 链接
使用当前模块作为前缀返回记录的类类型。因此,MyApp::Business::Account 的后代将显示为 MyApp::Business::AccountSubclass。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/inheritance.rb, line 240 def compute_type(type_name) if type_name.start_with?("::") # If the type is prefixed with a scope operator then we assume that # the type_name is an absolute reference. type_name.constantize else type_candidate = @_type_candidates_cache[type_name] if type_candidate && type_constant = type_candidate.safe_constantize return type_constant end # Build a list of candidates to search for candidates = [] name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" } candidates << type_name candidates.each do |candidate| constant = candidate.safe_constantize if candidate == constant.to_s @_type_candidates_cache[type_name] = candidate return constant end end raise NameError.new("uninitialized constant #{candidates.first}", candidates.first) end end