How to fetch a field through few tables.

7 views
Skip to first unread message

Dani Dani

unread,
Sep 14, 2011, 4:15:15 PM9/14/11
to rubyonra...@googlegroups.com
Hi, I have the following situation, 3 tables as follows:

TableA: TableB: TableC:
------- ------- -------
TableB_ID User_ID User_ID
User_Name

TableB_ID is a foreign key to TableB, User_ID in TableB and TableC is
foreign key to a table that play no role hier.

now I would like to display the User_Name field found in TableC in a e.g
index view. So I need to go from TableA with the TableB_ID foreign key
to get the User_ID field from TableB, then search for the record in
TableC that has this User_ID as one of its fields and get the User_Name.

This should look something like:

<td><%= TableA.TableB.TableC(where TableB.User_ID ==
TableC.User_ID).User_Name %></td>

I know that the syntax above is not correct but just to show what I'm
looking for.

Hope I made my self clear ;-)

Thanks
Dani

<td><%= course_attendee.sys_title.name unless
course_attendee.sys_title.nil? %></td>

--
Posted via http://www.ruby-forum.com/.

Tim Shaffer

unread,
Sep 14, 2011, 4:30:00 PM9/14/11
to rubyonra...@googlegroups.com
Do you have these set up as models in your application?

Perhaps you should take a look at the Rails Guides, specifically the ones on Models

Colin Law

unread,
Sep 15, 2011, 3:35:59 AM9/15/11
to rubyonra...@googlegroups.com

It might even be better to start with the one on ActiveRecord Associations
http://guides.rubyonrails.org/association_basics.html

Colin

--
gplus.to/clanlaw

Reply all
Reply to author
Forward
0 new messages