Forum: Ruby on Rails Relationship and reflection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7b43704dbe2fe5b5b0f43e4082956b92?d=identicon&s=25 Guillaume (Guest)
on 2006-06-09 00:19
Hello,

I have a question.
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.

Thanks for your help.
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-06-09 01:11
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
http://blog.hasmanythrough.com
7b43704dbe2fe5b5b0f43e4082956b92?d=identicon&s=25 Guillaume (Guest)
on 2006-06-09 17:11
Josh Susser wrote:
> 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
> http://blog.hasmanythrough.com

That's works very well.
Thanks a lot for your quick help, that's really helped me, I'm so happy.

Thank you very much again.
7b43704dbe2fe5b5b0f43e4082956b92?d=identicon&s=25 Guillaume (Guest)
on 2006-06-09 21:13
Hello,

I'm using a code similar to the previous one, slightly modified, like
that:

Person.reflect_on_all_associations.select { |r| @person.r.name.save }
where r.name = cars

When I'm using that code I expect ruby to interpret
@person.r.name.save to @person.cars.save, but it doesn't.

I even tried @person.#{r.name}.save, but with that '#' all my code fall
apart. I even try to concatenate with '+' but nothing worka.

I don't know how to make Ruby understand that I want to use the value
inside r.name.

If someone has an idea, please I need your help.


Thank you very much.
This topic is locked and can not be replied to.