Active Record Reflection¶ ↑
Reflection 实现了检查 Active Record 类和对象的关联(associations)和聚合(aggregations)的能力。例如,这些信息可以用于一个表单构建器,它接收一个 Active Record 对象,并根据其类型创建所有属性的输入字段,并显示与其他对象的关联。
MacroReflection 类包含 AggregateReflection 和 AssociationReflection 类的相关信息。
方法
- R
实例公共方法
reflect_on_aggregation(aggregation) 链接
返回指定 aggregation(使用符号)的 AggregateReflection 对象。
Account.reflect_on_aggregation(:balance) # => the balance AggregateReflection
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 70 def reflect_on_aggregation(aggregation) aggregate_reflections[aggregation.to_sym] end
reflect_on_all_aggregations() 链接
返回类中所有聚合的 AggregateReflection 对象数组。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 62 def reflect_on_all_aggregations aggregate_reflections.values end
reflect_on_all_associations(macro = nil) 链接
返回类中所有关联的 AssociationReflection 对象数组。如果你只想检查特定类型的关联,请将符号(:has_many, :has_one, :belongs_to)作为第一个参数传入。
示例
Account.reflect_on_all_associations # returns an array of all associations Account.reflect_on_all_associations(:has_many) # returns an array of all has_many associations
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 111 def reflect_on_all_associations(macro = nil) association_reflections = normalized_reflections.values association_reflections.select! { |reflection| reflection.macro == macro } if macro association_reflections end
reflect_on_all_autosave_associations() 链接
返回所有启用了 :autosave 选项的关联的 AssociationReflection 对象数组。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 131 def reflect_on_all_autosave_associations reflections = normalized_reflections.values reflections.select! { |reflection| reflection.options[:autosave] } reflections end
reflect_on_association(association) 链接
返回指定 association(使用符号)的 AssociationReflection 对象。
Account.reflect_on_association(:owner) # returns the owner AssociationReflection Invoice.reflect_on_association(:line_items).macro # returns :has_many
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 122 def reflect_on_association(association) normalized_reflections[association.to_sym] end
reflections() 链接
返回一个 Hash,其中键是反射的名称,值是 AssociationReflection 对象。
Account.reflections # => {"balance" => AggregateReflection}
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/reflection.rb, line 78 def reflections normalized_reflections.stringify_keys end