[Rails] Re: Relationship and reflection

Josh Susser
Thu, 08 Jun 2006 23:11:25 -0000

Guillaume wrote:
> I would like to know if there is a way with the reflection and
> respond_to? to know if there is a relationship between tables.
>
> When I say that I mean if inside a model there is a Has_many or
> belongs_to declaration. Because I try to get this information
> dynamically, during the execution of a script.
>
> If someone has an idea even if it''s not with the reflection,
because
> this way seemed for me a good one but maybe I''m wrong, and another
can
> do it.

If you want to find associations from Person to Car, try this:

Person.reflect_on_all_associations.select { |r| r.klass == Car }

--
Josh Susser