Using :include with find

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

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.