hiho. 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 ] -- Posted via http://www.ruby-forum.com/.
Edward Frederick
2006-Apr-10 19:22 UTC
[Rails] preloading child rows just a level deeper...
Gerald, Try googling for ''rails bottomless eager loading''--new feature in 1.1, I believe. -- Ed Frederick - edwardfrederick.com On 4/10/06, Gerald Schenke <realazrael@gmx.de> wrote:> hiho. > > 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 ] > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Edward Frederick
2006-Apr-10 19:24 UTC
[Rails] preloading child rows just a level deeper...
To follow up my previous reply:
try: A.find(:all, :include => { :b => { :c })
On 4/10/06, Gerald Schenke <realazrael@gmx.de>
wrote:> hiho.
>
> 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 ]
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>