# Using .find and :include with multiple levels of association

**URL:** <https://www.ruby-forum.com/t/using-find-and-include-with-multiple-levels-of-association/50485>\
**Category:** Rails\
**Created:** [November 20, 2005, 7:13am UTC](https://www.ruby-forum.com/t/using-find-and-include-with-multiple-levels-of-association/50485 "2005-11-20T07:13:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 20, 2005, 7:13am UTC](https://www.ruby-forum.com/t/using-find-and-include-with-multiple-levels-of-association/50485/1 "2005-11-20T07:13:56Z")

</div>

Hi,

I’ve tried to trim this example down to the minimum. Even though it  
looks  
complicated it is probably a fairly basic question about how  
associations  
and :include work. I want to do a find that is similar to the example  
below.  
The find statement doesn’t work because AttributeValue doesn’t have a  
direct  
association with Product: Product is one step further up the chain of  
models. Can I somehow write the find statement without resorting to  
:joins,  
raw SQL or looping through the results to make some checks?

Thanks,  
Peter

Class Product \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :attributes  
end

Class Attribute \< ActiveRecord::Base  
#not all attributes will be associated with a product  
has\_and\_belongs\_to\_many :products  
has\_many :attribute\_values  
end

Class AttributeValue \< ActiveRecord::Base  
belongs\_to :attribute

# cannot create a belongs\_to association with Variation

end

Class Variation \< ActiveRecord::Base  
has\_many :attribute\_values  
belongs\_to :product

def distinguishing\_attribute\_values  
AttributeValue.find(:all, :include=\>[:attribute, :products],  
:conditions=\>"  
product.id=’#{product.id}’");  
end

end

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 20, 2005, 8:06am UTC](https://www.ruby-forum.com/t/using-find-and-include-with-multiple-levels-of-association/50485/2 "2005-11-20T08:06:08Z")

</div>

Hi,

The following works but I just keep thinking that this should be simpler

VirtualAttributeValue.find(:all, :include =\> :virtual\_attribute,  
:joins =\> ‘LEFT OUTER JOIN products\_virtual\_attributes ON  
products\_virtual\_attributes.virtual\_attribute\_id =  
virtual\_attributes.id’,  
:conditions=\>  
“products\_virtual\_attributes.product\_id=#{product.id[http://product.id](http://product.id)  
}  
AND virtual\_attribute\_values.variation\_id = ‘#{id}’”)

Any suggestions to make is simpler without that :joins? seems like I  
should  
be able to take better advantage of the existing chain of models through  
associations.

-Peter

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 20, 2005, 8:26am UTC](https://www.ruby-forum.com/t/using-find-and-include-with-multiple-levels-of-association/50485/3 "2005-11-20T08:26:13Z")

</div>

I have it down to the following statement. Maybe this is the best it can  
be?

virtual\_attribute\_values.find(:all, :include =\> :virtual\_attribute,  
:joins =\> ‘LEFT OUTER JOIN products\_virtual\_attributes ON  
products\_virtual\_attributes.virtual\_attribute\_id =  
virtual\_attributes.id’,  
:conditions=\>  
“products\_virtual\_attributes.product\_id=#{product.id[http://product.id](http://product.id)  
}”)
