Forum: Ruby on Rails preloading child rows just a level deeper...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Gerald Schenke (Guest)
on 2006-04-10 22:30

following problem.
i have 3 tables in my database with each representing an object.
lets say
A belongs_to B belongs_to C.

when i do
my_a = A.find(:all)
i can access B as my_a.b and C as my_a.b.c
thats fine but rails querys the database each time i do that

so is use
my_a = A.find(:all, :include => :b)
works fine for my_a.b but not for my_a.b.c

so i tried
my_a = A.find(:all, :include => [:b, :c])
but that gives an error:
"Association named 'c' was not found; perhaps you misspelled it?"

well, no i didnt misspelled, but table A dont has an c_id. only table B
knows to what c it belongs
no wonder there is an error ... :/

question is:
Can i preload the rows of C like i do it with the rows in B when i query
table A?

i hope i made it clear enough... :/

[ No, i cant/dont want to use STI ]
Edward F. (Guest)
on 2006-04-10 23:22
(Received via mailing list)

Try googling for 'rails bottomless eager loading'--new feature in 1.1,
I believe.


Ed Frederick -
Edward F. (Guest)
on 2006-04-10 23:25
(Received via mailing list)
To follow up my previous reply:

try: A.find(:all, :include => { :b => { :c })
Gerald S. (Guest)
on 2006-04-10 23:40
thank you so much!
This topic is locked and can not be replied to.