# Tricky include question

**URL:** <https://www.ruby-forum.com/t/tricky-include-question/146742>\
**Category:** Rails\
**Created:** [August 24, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/tricky-include-question/146742 "2008-08-24T22:52:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 24, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/tricky-include-question/146742/1 "2008-08-24T22:52:00Z")

</div>

I’ve been here before and maybe I’m trying to do something that is not  
possible. I am trying to include tables that are associated to other  
tables in the actual include statement but not to the original table  
call. In this case the furnii table.

this is my original query

@furnii\_from\_furnii = Furni.find :all, :include =\>[:raider, {:imageasset  
=\> :skin}], :conditions =\> “”+Furni.conditions\_by\_like(@search)+" OR "  
+Raider.conditions\_by\_like(@search) + “OR”  
+Skin.conditions\_by\_like(@search) + “”

now I need to include 2 more tables skintype and skinstyle. these 2  
tables are not associated to either furnii or imageassets but have an  
association to the skin table, which is associated to furnii through  
imageassets.

can I group them in the same query? or do I have to do a separate query  
for skintype and skinstyle and then join it at the end?

Sorry for my premature explanation.

(I have simplified my models to reflect the above question)  
my models look like this:

class Furni \< ActiveRecord::Base  
belongs\_to :imageasset  
end

class Imageasset \< ActiveRecord::Base  
has\_one :skin  
end

class Skin \< ActiveRecord::Base  
belongs\_to :imageasset  
belongs\_to :skinstyle  
belongs\_to :skintype  
end

class Skintype \< ActiveRecord::Base  
has\_one :skin  
end

class Skinstyle \< ActiveRecord::Base  
has\_one :skin  
end

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 24, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/tricky-include-question/146742/2 "2008-08-24T23:45:25Z")

</div>

On Aug 24, 9:52 pm, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> +Skin.conditions\_by\_like(@search) + “”
> 
> now I need to include 2 more tables skintype and skinstyle. these 2  
> tables are not associated to either furnii or imageassets but have an  
> association to the skin table, which is associated to furnii through  
> imageassets.

Sure you can, you can nest includes as deep as you want. I’ve got some  
examples at

> **[Nested includes and joins - Space Vatican](https://www.spacevatican.org/2008/8/8/nested-includes-and-joins/)**
>
> Nested eager loads are a not entirely obvious bit of rails syntax. It’s not hard once you get it though. In a nutshell you have to tell …

Fred

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 25, 2008, 12:39am UTC](https://www.ruby-forum.com/t/tricky-include-question/146742/3 "2008-08-25T00:39:29Z")

</div>

Frederick C. wrote:

> On Aug 24, 9:52ï¿½pm, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > +Skin.conditions\_by\_like(@search) + “”
> > 
> > now I need to include 2 more tables skintype and skinstyle. these 2  
> > tables are not associated to either furnii or imageassets but have an  
> > association to the skin table, which is associated to furnii through  
> > imageassets.
> 
> Sure you can, you can nest includes as deep as you want. I’ve got some  
> examples at  
> [Nested includes and joins - Space Vatican](http://www.spacevatican.org/2008/8/8/nested-includes-and-joins)
> 
> Fred

Thanks for the link. I’m still not sure on how to join the 2 tables in  
question.

I tried :include =\>[:raider, {:imageasset =\> {:skintype =\>:skin}] but  
that is not finding the association  
I tried :include =\>[:raider, {:imageasset =\> {:skin =\>:skintype}] that  
is not working ether.

skin belongs to skintype so I’m not sure how to load it

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 25, 2008, 1:56am UTC](https://www.ruby-forum.com/t/tricky-include-question/146742/4 "2008-08-25T01:56:37Z")

</div>

Sam G. wrote:

> I tried :include =\>[:raider, {:imageasset =\> {:skintype =\>:skin}] but  
> that is not finding the association  
> I tried :include =\>[:raider, {:imageasset =\> {:skin =\>:skintype}] that  
> is not working ether.
> 
> skin belongs to skintype so I’m not sure how to load it

Please disregard the question above. Your examples cleared it all up.  
Thanks again for your help.
