# Trouble with :include

**URL:** <https://www.ruby-forum.com/t/trouble-with-include/137670>\
**Category:** Rails\
**Created:** [May 15, 2008, 6:48am UTC](https://www.ruby-forum.com/t/trouble-with-include/137670 "2008-05-15T06:48:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [May 15, 2008, 6:48am UTC](https://www.ruby-forum.com/t/trouble-with-include/137670/1 "2008-05-15T06:48:39Z")

</div>

For some reason it seems that :include is not working for a couple of  
my associations. Here are the models I’m using:

##### MODELS

class Registration \< ActiveRecord::Base  
has\_many :events, :foreign\_key =\> :user\_id  
end

# Note the single table inheritance

class User \< Registration  
end

class Event \< ActiveRecord::Base  
belongs\_to :user  
has\_one :image, :as =\> :imageable, :dependent =\> :destroy  
end

class Image \< ActiveRecord::Base  
belongs\_to :imageable, :polymorphic =\> true  
end

##### 

I’m using single table inheritance with Registrations because there  
are 2 types of users on the site: User and TmpUser. A TmpUser is  
essentially a shell that should not have any validation when saved and  
a different set of filters.

When I run the following code it does not load either the image or  
user association in the query:

evts = Event.find(:all, :include =\> [:image, :user], :conditions =\>  
“start IS NOT NULL AND start \>= CURDATE()”)

So it runs a new query for each of these statements:

evts.first.image  
evts.first.user

What am I doing wrong?

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [May 15, 2008, 7:00am UTC](https://www.ruby-forum.com/t/trouble-with-include/137670/2 "2008-05-15T07:00:57Z")

</div>

Does the Users table have a column named “type”? that is required for  
STI

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [May 15, 2008, 7:54am UTC](https://www.ruby-forum.com/t/trouble-with-include/137670/3 "2008-05-15T07:54:51Z")

</div>

Yes the Registrations table has a column named “type”. Since Users  
inherits from Registrations we don’t need a Users table (right?).

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [May 15, 2008, 9:02am UTC](https://www.ruby-forum.com/t/trouble-with-include/137670/4 "2008-05-15T09:02:25Z")

</div>

Correct, I wasn’t thinking far enough up the chain.

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [May 16, 2008, 4:15am UTC](https://www.ruby-forum.com/t/trouble-with-include/137670/5 "2008-05-16T04:15:29Z")

</div>

Does anybody know why I’m having this problem?

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [May 16, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/trouble-with-include/137670/6 "2008-05-16T21:12:35Z")

</div>

My guess is that Rails is lost because the STI with non-standard fks  
is confusing. It’s able to navigate things better once you have the  
instance back. You might give Rails a little help if you have a  
require ‘registration’ at the top of your event.rb. That should help  
rails find the STI subclasses before you try your Event.find.

Have you checked the log after running the initial query to see if it  
is trying to do the joins?

I think one implication of your model is that only Users get to have  
Events (at least Rails might think so). Is that true?
