# Question about Associations and Eager Loading

**URL:** <https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710>\
**Category:** Rails\
**Created:** [July 25, 2006, 2:14am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710 "2006-07-25T02:14:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 25, 2006, 2:14am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/1 "2006-07-25T02:14:56Z")

</div>

Hey everyone,

I’m currently making an application that allows users to create custom  
fields that have attributes associated with each field.

They can then attach instances of these fields (each with their own  
separate attribute instances) to their account.

So, it looks like this:

```
     Fields
 / \

```

Field Instances Field Attributes  
\ /  
Field Attribute Instances

Where Field Instances and Field Attributes both belong to Field, and  
have  
many Field Attribute Instances. So the kicker here is that Field  
Attribute Instances are dependent on both Field Instances and Field  
Attributes.

I need to get an array that looks like the following:  
Fields.FieldInstances.FieldAttributes.FieldAttributeInstances

Right now, I’m doing something like the following:  
Field.find(:all, :include =\> { :field\_instances =\> { :field\_attributes  
=\>  
{ :field\_attribute\_instances } } }) which gives me the order I want.  
However, I see no way to get field\_attribute\_instances dependent on  
BOTH  
field\_attributes and field\_instances (instead of just field\_attributes,  
as  
it currently is)

In fact, even if I specify that in the :conditions option, Active Record  
will ignore it and add any and all field\_attribute\_instances that are  
dependent on field\_attribute (even if they don’t match field\_instances).

If the order in which I include things changes to FieldInstances =\>  
FieldAttributeInstances =\> FieldAttribute then I get the results I need,  
but not the array I need.

Any suggestions on how I can go about fixing this problem?

Thanks.

---

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 25, 2006, 2:44am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/2 "2006-07-25T02:44:24Z")

</div>

> Field.find(:all, :include =\> { :field\_instances =\>  
> { :field\_attributes =\>  
> { :field\_attribute\_instances } } })

This looks right to me. Have you tried adding a compound condition:  
:conditions =\> [‘field\_instances.id = ? AND field\_attributes.id =?,  
3, 4’]

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 25, 2006, 2:59am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/3 "2006-07-25T02:59:52Z")

</div>

> > Field.find(:all, :include =\> { :field\_instances =\>  
> > { :field\_attributes =\>  
> > { :field\_attribute\_instances } } })
> 
> This looks right to me. Have you tried adding a compound condition:  
> :conditions =\> [‘field\_instances.id = ? AND field\_attributes.id =?,  
> 3, 4’]
> 
> ```
> - dan
> 
> ```

Hi Dan,

Yes, I’ve tried that (actually, not exactly that, but :conditions =\>  
‘field\_attribute\_instances\_field\_attributes.field\_instance\_id =  
field\_instances.id’, as well as a bit of a hack - :joins =\> ‘AND  
field\_attribute\_instances\_field\_attributes.field\_instance\_id =  
field\_instances.id’)

Running the query in my MySQL client gives me the correct results (ie.  
field\_attribute\_instances are dependent on both field\_attributes and  
field\_instances).

However, it seems that Active Record is interpreting the :includes  
option  
a bit too literally for my needs, basically doing something like  
(pseudocode):

if field\_attribute\_instances.field\_attribute\_id == field\_attributes.id  
Field.FieldInstance.FieldAttribute \<\< field\_attribute\_instances  
end

(where the condition _should_ be:  
field\_attribute\_instances.field\_attribute\_id == field\_attributes.id &&  
field\_attribute\_instances.field\_instance\_id == field\_instances.id)

Thanks again for the help

- Marcel

---

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 25, 2006, 4:08am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/4 "2006-07-25T04:08:57Z")

</div>

Sorry, I meant includes overriding selects. It could be overriding  
conditions too.

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 25, 2006, 3:49am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/5 "2006-07-25T03:49:58Z")

</div>

I find I can usually work this out by working with script/console and  
watching development.log to see what SQL ActiveRecord is generating.  
There’s at least one open bug regarding the includes overriding the  
conditions. If you can confirm that that is happening here too, you  
should file a bug report.

In the meantime, I don’t understand why you would need to bother  
with the joins, as AR passes conditions right through (after  
sanitizing the variables).

[http://dev.rubyonrails.org/ticket/5371](http://dev.rubyonrails.org/ticket/5371)

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 26, 2006, 7:49am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/6 "2006-07-26T07:49:01Z")

</div>

I would:

1. Post the bug to [dev.rubyonrails.com](http://dev.rubyonrails.com), attaching your two test  
files. I can’t see how you can argue it’s a feature.

2. Simply use a find\_by\_sql call to get exactly the functionality you  
want.

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 26, 2006, 3:45am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/7 "2006-07-26T03:45:56Z")

</div>

> Sorry, I meant includes overriding selects. It could be overriding  
> conditions too.
> 
> ```
> - dan
> 
> ```
> 
> –  
> Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

Hey Dan,

Thanks again for your help. I’m wondering though, if it’s a bug or  
expected behaviour. I’ve narrowed the code down to a (fairly) simple  
testcase and posted it on [http://www.marcelguzman.com/rails/](http://www.marcelguzman.com/rails/) (the  
migration generates 6 rows of simple test data, and the output from  
console shows the bug).

Any idea if I’d be able to get around that or not?

If I switch the order of includes to :field\_instances =\>  
:field\_attribute\_instances =\> :field\_attributes , then it grabs  
everything  
perfectly, but then I’m stuck becasue I need to access the data in the  
following order:  
field\_instances =\> field\_attributes =\> field\_attribute\_instances

I suppose I could try using something like an ORDER BY clause and then  
looping through, catching when field\_attribute\_id changes. But that  
does  
seem incredibly hacky to me and I’m wondering if there’s a better way.

Thanks again,  
Marcel

---

<div class="post-metadata">

**Author:** ![Marcel\_Guzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@Marcel\_Guzman](https://www.ruby-forum.com/u/Marcel_Guzman)\
**Post date:** [July 27, 2006, 1:10am UTC](https://www.ruby-forum.com/t/question-about-associations-and-eager-loading/99710/8 "2006-07-27T01:10:35Z")

</div>

> [http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000  
> Hi Dan, thanks again for your help - you’ve been of great assistance 🙂

1. I will definitely do that tonight, but I want to prepare some more  
detailed test data first to make the bug more evident.

2. I could do that - but is there any way to use find\_by\_sql such that  
it  
will still allow me to use the association collections, or will I have  
to  
manually process the returned data?

Thanks,  
Marcel
