Forum: Ruby on Rails multi level :includes for preloading objects from database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C75369f25cd6b34076875bdc308c2aca?d=identicon&s=25 John Russell (jjrussell)
on 2006-04-20 21:55
I am using :include => :assocation to preload data that I know will be
used later in the UI over and over.  However I have data like that that
is an association of one of the associations.  How would i preload that
data too?

e.g.

grant has_many :people

and

person belongs_to :group

I can't include the :group association in a Grant.find because its not
an association of grant, but I use grant.person.group all over the place
so it hits the db every time.  How can I include group in the
Grant.find?

thanks.
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-04-20 22:02
(Received via mailing list)
On 4/20/06, John Russell <jjrussell@gmail.com> wrote:
>
> person belongs_to :group
>
> I can't include the :group association in a Grant.find because its not
> an association of grant, but I use grant.person.group all over the place
> so it hits the db every time.  How can I include group in the
> Grant.find?
>
> thanks.
>

You can nest the include statements, as of Rails 1.1:
:include => [:grants, {:people => :groups}]

--Wilson.
This topic is locked and can not be replied to.