| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) | |
|
vague™
Join Date: Mar 2004
Location: Glasgow
Posts: 5,337
|
Rails joining
Hey, quick question regarding joining while using ActiveRecord's find method: If i have the following models setup: Course - has_many :course_dates CourseDate - belongs_to :course - has_one :venue Venue - belongs_to :course_date Is there any way to get Rails to automatically join all three tables using find without having to explicitly write the SQL join syntax? The following will work, but I can't see any way to connect the venue table as well (which is related to course dates, not the course itself). Quote:
Cheers for any help - just thought I'd ask before resorting to writing the SQL. |
|
|
|
|
|
|
#2 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
Well, with a normal find, there isn't anything stopping you from doing the following to get at a venue: Code:
So I assume you are trying to do some kind of eager loading to reduce the number of queries? You could try this change to your course model: Code:
Then when loading courses, specify :include => :course_dates as normal. Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
|
|
#3 (permalink) | |
|
vague™
Join Date: Mar 2004
Location: Glasgow
Posts: 5,337
|
Hi Luke, cheers for the reply. Yes, I'm trying to reduce the number of queries necessary. Tried the change you suggested but it didn't seem to have the desired effect. Having the join written explicitly isn't too big a deal, so i'll use that for now. Quote:
|
|
|
![]() |