# Nested include

**URL:** <https://www.ruby-forum.com/t/nested-include/145227>\
**Category:** Rails\
**Created:** [August 6, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/nested-include/145227 "2008-08-06T23:35:49Z")\
**Posts on this page:** 8\
**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 6, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/nested-include/145227/1 "2008-08-06T23:35:49Z")

</div>

I’m trying to access data from the skins table through a nested include  
with no luck. The association is all through imageasset but I do not  
know how to figure it out. Any help is highly appreciated.

######the controller code#######  
@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) + “”

#######I have 4 models set up this way (did only include the necessary  
code )#######  
class Furni \< ActiveRecord::Base  
belongs\_to :imageasset  
belongs\_to :raider, :foreign\_key =\> “original\_raider\_id”  
end  
class Raider \< ActiveRecord::Base  
belongs\_to :imageasset  
end  
class Skin \< ActiveRecord::Base  
belongs\_to :imageasset  
end

class Imageasset

## nothing

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 7, 2008, 3:24am UTC](https://www.ruby-forum.com/t/nested-include/145227/2 "2008-08-07T03:24:59Z")

</div>

On Aug 6, 10:35 pm, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m trying to access data from the skins table through a nested include  
> with no luck. The association is all through imageasset but I do not  
> know how to figure it out. Any help is highly appreciated.
> 
> ######the controller code#######  
> @furnii\_from\_furnii = Furni.find :all, :include =\> [{:raider =\>  
> :imageasset}, :skin ]

This won’t work unless Furni has an association called imageasset,  
which it doesn’t. If you want Activerecord to go through image asset  
you’re going to need to either write

:include =\> [{:raider =\> :imageasset}, {:imageasset =\> :skin}]

or

:include =\> {:raider =\> {:imageasset =\> :skin}}

Either way you have to have a skin (or skins, in which case  
replace :skin by :skins in the above examples) association on  
imageasset.

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 7, 2008, 4:12am UTC](https://www.ruby-forum.com/t/nested-include/145227/3 "2008-08-07T04:12:38Z")

</div>

Fred,

Tried both your options and I’m still getting the error “Association  
named ‘skin’ was not found; perhaps you misspelled it?”

furni has the association imageasset as belongs\_to :imageasset. I’m not  
sure that changes anything

> This won’t work unless Furni has an association called imageasset,  
> which it doesn’t. If you want Activerecord to go through image asset  
> you’re going to need to either write
> 
> :include =\> [{:raider =\> :imageasset}, {:imageasset =\> :skin}]
> 
> or
> 
> :include =\> {:raider =\> {:imageasset =\> :skin}}
> 
> Either way you have to have a skin (or skins, in which case  
> replace :skin by :skins in the above examples) association on  
> imageasset.
> 
> 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 7, 2008, 4:18am UTC](https://www.ruby-forum.com/t/nested-include/145227/4 "2008-08-07T04:18:04Z")

</div>

I am Trying to get the data in the skins table. the Association between  
furni, raider and skin is through imageasset id. Hope I make sense.

Sam G. wrote:

> Fred,
> 
> Tried both your options and I’m still getting the error “Association  
> named ‘skin’ was not found; perhaps you misspelled it?”
> 
> furni has the association imageasset as belongs\_to :imageasset. I’m not  
> sure that changes anything
> 
> > This won’t work unless Furni has an association called imageasset,  
> > which it doesn’t. If you want Activerecord to go through image asset  
> > you’re going to need to either write
> > 
> > :include =\> [{:raider =\> :imageasset}, {:imageasset =\> :skin}]
> > 
> > or
> > 
> > :include =\> {:raider =\> {:imageasset =\> :skin}}
> > 
> > Either way you have to have a skin (or skins, in which case  
> > replace :skin by :skins in the above examples) association on  
> > imageasset.
> > 
> > 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 7, 2008, 4:30am UTC](https://www.ruby-forum.com/t/nested-include/145227/5 "2008-08-07T04:30:00Z")

</div>

On Aug 7, 3:18 am, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am Trying to get the data in the skins table. the Association between  
> furni, raider and skin is through imageasset id. Hope I make sense.

When I said

> This won’t work unless Furni has an association called imageasset,  
> which it doesn’t  
> I should of couse have said that ImageAsset should have a skin (or  
> possibly a skins association). You won’t be getting anywhere without  
> one.

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 7, 2008, 4:35am UTC](https://www.ruby-forum.com/t/nested-include/145227/6 "2008-08-07T04:35:25Z")

</div>

So in other words you mean that the imageasset model should have a  
has\_many :skin or should the association be in the imageasset table with  
a skin id?

Frederick C. wrote:

> On Aug 7, 3:18ï¿½am, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am Trying to get the data in the skins table. the Association between  
> > furni, raider and skin is through imageasset id. Hope I make sense.
> 
> When I said
> 
> > This won’t work unless Furni has an association called imageasset,  
> > which it doesn’t  
> > I should of couse have said that ImageAsset should have a skin (or  
> > possibly a skins association). You won’t be getting anywhere without  
> > one.
> 
> 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 7, 2008, 4:55am UTC](https://www.ruby-forum.com/t/nested-include/145227/7 "2008-08-07T04:55:24Z")

</div>

Got it.

this works to by the way :include =\>[:raider, {:imageasset =\> :skin}]

Frederick C. wrote:

> On Aug 7, 3:35ï¿½am, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So in other words you mean that the imageasset model should have a  
> > has\_many :skin or should the association be in the imageasset table with  
> > a skin id?
> 
> Either a has\_many or a has\_one. you need to tell activerecord how to  
> find a skin from an imageasset (it isn’t enough to just say that  
> imageasset belongs\_to :skin, rails won’t guess the association in the  
> other direction).
> 
> 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 7, 2008, 4:52am UTC](https://www.ruby-forum.com/t/nested-include/145227/8 "2008-08-07T04:52:47Z")

</div>

On Aug 7, 3:35 am, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So in other words you mean that the imageasset model should have a  
> has\_many :skin or should the association be in the imageasset table with  
> a skin id?

Either a has\_many or a has\_one. you need to tell activerecord how to  
find a skin from an imageasset (it isn’t enough to just say that  
imageasset belongs\_to :skin, rails won’t guess the association in the  
other direction).

Fred
