0

ich habe:Wie kann ich Name von durch Relation von has_many Relation in Schienen bekommen?

class Foo 
    has_many :bar 
    has_many :baz, through :bar 
end 


class FooTwo 
    has_many :barTwo 
    has_many :baz, through :barTwo 
end 

I abble sein müssen durch Beziehung baz Assoziation zu erhalten, wie:

Foo.first.baz.relation_through #<=> Foo.first.bar 
FooTwo.first.baz.relation_through #<=> Foo.first.barTwo 

wenn es unmöglich ist, kann ich nur den Namen? wie:

Foo.first.baz.get_relation_through_name # "bar" 
FooTwo.first.baz.get_relation_through_name # "barTwo" 

Antwort

2

Versuchen Sie, diese

Foo.reflect_on_all_associations.find { |association| association.name == :baz}.options[:through]