# Using :include with find

**URL:** <https://www.ruby-forum.com/t/using-include-with-find/69708>\
**Category:** Rails\
**Created:** [September 23, 2006, 2:18am UTC](https://www.ruby-forum.com/t/using-include-with-find/69708 "2006-09-23T02:18:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 23, 2006, 2:18am UTC](https://www.ruby-forum.com/t/using-include-with-find/69708/1 "2006-09-23T02:18:45Z")

</div>

I’m having trouble trying to figure out how to render data when you  
have multiple tables and a joining table with data between them. I’ll  
use the Rails Recipe #22 as an example to illustrate my trouble.

The concept is that you have readers and magazines and subscriptions:  
–A reader has many subscriptions  
–A reader has many magazines through subscriptions  
–A magazine has many subscriptions  
–A magazine has many readers through subscriptions  
–A subscription belongs to both a reader and a magazine

In the example the subscription also has some data such as number of  
issues. My problem is this how do I for a specific reader show each  
magazine he subscribes to and the number of issues he bought. This  
requires data from both the subscription and magazine model in one  
loop.

So in my controller I try something like this for the show method:  
@reader = Reader.find(params[:id])  
@subs = @reader.magazines.find(:all, :include =\> :subscriptions)

In my view code I spit out some data with the @reader object and then  
try to show the list of magazines and the number of issues for each  
magazine (Basically there is a one-to-one relationship between a  
magazine and an idividual subscription so I want to display them in one  
row). View code below:

\<% for each sub in @subs %\>  
Magazine: \<%= sub.mag\_name %\> / Issues: \<%=  
sub.subscription.num\_of\_issues %\>  
\<% end %\>

What happens is that I get an error of undefined method for  
num\_of\_issues instead of the data in the num\_of issues attribute. I’m  
assuming that this is just syntax I’ve misunderstood. If I run the  
find in the console it appears to be pulling back all of the data I  
just can’t figure out how to access each bit in a loop. I’ve searched  
all over the web this afternoon and find tons of examples that show  
controller code or console statements, but rarely code from the  
controller and the view. Any help would be greatly appreciated.

–Eric

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 23, 2006, 1:36pm UTC](https://www.ruby-forum.com/t/using-include-with-find/69708/2 "2006-09-23T13:36:11Z")

</div>

removed\_email\_address@domain.invalid wrote:

> So in my controller I try something like this for the show method:  
> @reader = Reader.find(params[:id])  
> @subs = @reader.magazines.find(:all, :include =\> :subscriptions)

That’s a bit confusing @reader.magazines.find is returning an array of  
magazines, calling it @subs is weird.

> Magazine: \<%= sub.mag\_name %\> / Issues: \<%=  
> sub.subscription.num\_of\_issues %\>  
> A magazine has many subscriptions, so magazine.subscription doesn’t  
> exist. you want to do something to magazine.subscriptions  
> If however you just want to count the number of subscriptions for each  
> magazine that the reader subscribes to then you a doing a lot of work  
> since you are pulling all these subscriptions across from the db into  
> rails just to count them (have a look at the ActiveRecord::Calculations)

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 23, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/using-include-with-find/69708/3 "2006-09-23T14:47:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’m having trouble trying to figure out how to render data when you  
> have multiple tables and a joining table with data between them. I’ll  
> use the Rails Recipe #22 as an example to illustrate my trouble.
> 
> The concept is that you have readers and magazines and subscriptions:  
> –A reader has many subscriptions  
> –A reader has many magazines through subscriptions  
> –A magazine has many subscriptions  
> –A magazine has many readers through subscriptions  
> –A subscription belongs to both a reader and a magazine

Hi Eric

I don’t know if it answers your question but this is what I do… and it  
works

In your models  
reader  
has\_many : subscriptions  
has\_many :magazines, :through =\> : subscriptions

magazine  
has\_many : subscriptions  
has\_many :readers, :through =\> : subscriptions

subscription  
belongs\_to :reader  
belongs\_to :magazine

in your controller action list for example  
…  
@subscriptions\_pages, @subscriptions = paginate :subscriptions, :order  
=\> sort, :conditions =\> [… to be defined…], :include=\>[reader,  
:magazine ], :per\_page =\> subscriptions\_per\_page  
…

(note : you can even include upward levels… like :include=\>[reader,  
{:magazine=\>:group}] if a group has many magazins… isn’t wonderful ?)

in your view list.rhtml  
…

\<% for subscription in @ subscriptions %\> \<%= subscription.reader.name %\> \<%= subscription. magazine.title %\> .... \<% end %\>

I build the conditions with the EZ-Where plugin

[http://brainspl.at/articles/2006/06/30/new-release-of-ez\_where-plugin](http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin)

very very useful to build complex where conditions… I can send you  
some examples

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/using-include-with-find/69708/4 "2006-09-25T17:24:56Z")

</div>

Thanks for the responses. I actually got things working using  
something like this: sub.subscriptions.first.num\_of\_issues. I’ll be  
taking a look at both of your suggestions though going forward,  
especially the EZ-Where plugin.
