ActiveRecord :include

閲覧: 32 回
最初の未読メッセージにスキップ

CLR

未読、
2008/06/13 15:38:002008/06/13
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

未読、
2008/06/14 7:29:402008/06/14
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

未読、
2008/06/14 10:19:052008/06/14
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

未読、
2008/06/14 14:28:072008/06/14
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

未読、
2008/06/15 10:21:242008/06/15
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

未読、
2008/06/17 20:25:292008/06/17
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

全員に返信
投稿者に返信
転送
新着メール 0 件