If you have set up the associations (belongs_to and has_many) then if you
have a Child object child you can just say child.mother to get the mother.
Similarly if you have a mother object you can say mother.children to get the
children.  It is all done by magic.
Colin

2009/5/12 Anthony Ward <rails-mailing-l...@andreas-s.net>

>
> Hi,
>
> I'm trying to understand belongs_to and has_many etc... I understand the
> relationship but I'm trying to figure how this can help me
>
> for example I have two tables
> i have 2 tables called mothers and another called childrens
> in this childrens model i have this
>
> ###Childrens model
> belongs_to: mother
> ..
> ..
>
> ####
> now in the
> children controller
> I want to retrive the mother name currently I'm doing this way
>
> def index ##show all the childrens and their mother's name
>  @childrens = Childrens.find(
>     :all,
>     :select=>"mothers.name AS mother_name, childrens.* ",
>     :join=>"LEFT JOIN mothers ON mothers.id = childrens.mother_id"
>  )
> end
>
> I was just curious because of the belongs_to I can skip this left join
> on tables
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to