我有:
class Foo
has_many :bar
has_many :baz, through :bar
end
class FooTwo
has_many :barTwo
has_many :baz, through :barTwo
end
我需要能够完成与baz协会的关系,例如:
Foo.first.baz.relation_through #<=> Foo.first.bar
FooTwo.first.baz.relation_through #<=> Foo.first.barTwo
如果不可能,我能得到这个名字吗?喜欢:
Foo.first.baz.get_relation_through_name # "bar"
FooTwo.first.baz.get_relation_through_name # "barTwo"
答案 0 :(得分:2)
试试这个
Foo.reflect_on_all_associations.find { |association| association.name == :baz}.options[:through]
答案 1 :(得分:1)
您正在寻找:http://apidock.com/rails/ActiveRecord/Reflection/ClassMethods/reflect_on_all_associations
Foo.reflect_on_all_associations(:has_many)
然后选择这个名字。