ActiveRecord :include

32 views
Skip to first unread message

CLR

unread,
Jun 13, 2008, 3:38:00 PM6/13/08
to maine-ruby-...@googlegroups.com
I have a bunch of models that have_many :through to each other. In the
controller I have this:

@data = ModelOne.find :all, :include => [ :model_twos => [ :model_threes
=> [ :model_fours ] ] ], :conditions => ...

Which gives me this error:

undefined method `loaded' for #<Array:0x7f2c9d2447f0>


Has anyone else run into this problem using :include?

Thanks!
-CLR

eric draut

unread,
Jun 14, 2008, 7:29:40 AM6/14/08
to maine-ruby-...@googlegroups.com
This might be an email typo, but shouldn't that say

:include => {:model_twos => {:model_threes => :model_fours}}
?
curly braces around the hashes instead of straight braces, no braces around a single association like :model_fours.

Eric

Whitelancer

unread,
Jun 14, 2008, 10:19:05 AM6/14/08
to Maine Ruby Users Group
Yeah, that error could be from the { vs [ issue. Or Hash[] works.

Why aren't you
doing :include=>[:model_twos, :model_threes, :model_fours] ? I've
never that nested array syntax thing before. What's that do?

eric draut

unread,
Jun 14, 2008, 2:28:07 PM6/14/08
to maine-ruby-...@googlegroups.com
an array of associations selects each array member and associates it with the parent model in a flat list fashion. A hash selects the associations in the key list of the hash, then finds the values as they associate to the key, not the model on which you are calling AR#find

So:

Student.find(:all, :include => {:courses => {:instructor => {:office_building => [:address, :manager]}}}

will find all students. For each student it will also fetch the 'courses' for that student. For each of those courses it will also fetch the instructor for that course. And while it's doing that it will grab the office_building for the instructor. Then it will grab the address of the office building and the manager of the office building.

A simple array example:

Student.find(:all, :include => [:courses, :addresses, :dorm_room, {:advisor => :office_building}]

This will find each student, and for each student it will also fetch that student's course list, that student's address, that student's dorm room, that student's advisor, and the advisor's office building.

So arrays are lists of associations on the model (or key model if it's a hash). Hashes are also lists of associations on the model, but only the keys are associated to the model. The values are associated to the key model.

I'm not sure I said that very clearly, I guess I made up for it in quantity.

-Eric

Whitelancer

unread,
Jun 15, 2008, 10:21:24 AM6/15/08
to Maine Ruby Users Group
Thanks Eric, I understand what you mean exactly.

Seems like that works pretty nicely, but I can only imagine the size
of those SQL queries! =)

On Jun 14, 2:28 pm, "eric draut" <edr...@gmail.com> wrote:
> an array of associations selects each array member and associates it with
> the parent model in a flat list fashion. A hash selects the associations in
> the key list of the hash, then finds the values as they associate to the
> key, not the model on which you are calling AR#find
>
> So:
>
> Student.find(:all, :include => {:courses => {:instructor =>
> {:office_building => [:address, :manager]}}}
>
> will find all students. For each student it will also fetch the 'courses'
> for that student. For each of those courses it will also fetch the
> instructor for that course. And while it's doing that it will grab the
> office_building for the instructor. Then it will grab the address of the
> office building and the manager of the office building.
>
> A simple array example:
>
> Student.find(:all, :include => [:courses, :addresses, :dorm_room, {:advisor
> => :office_building}]
>
> This will find each student, and for each student it will also fetch that
> student's course list, that student's address, that student's dorm room,
> that student's advisor, and the advisor's office building.
>
> So arrays are lists of associations on the model (or key model if it's a
> hash). Hashes are also lists of associations on the model, but only the keys
> are associated to the model. The values are associated to the key model.
>
> I'm not sure I said that very clearly, I guess I made up for it in quantity.
>
> -Eric
>
> On Sat, Jun 14, 2008 at 10:19 AM, Whitelancer <whitelanc...@gmail.com>

CLR

unread,
Jun 17, 2008, 8:25:29 PM6/17/08
to maine-ruby-...@googlegroups.com
So the syntax wasn't the issue. I did try hashes instead of arrays, but
AR is iterating through the enumerations in the same way, so that didn't
make a difference.

Debugger led me to discover that ModelThree in the example was being
generated as an Array, not as an Association, and then 'loaded' was
being called on the Array, which doesn't have that method.

Edge didn't fix this (same error) but it is too deep in the bowels of AR
for me to venture any futher. Unfortunately, I had to resort to
constructing the SQL by hand.

(v_v)

Thanks!
-Casey

Reply all
Reply to author
Forward
0 new messages