# Has many :through, accessing join model attribute (newbie)

**URL:** <https://www.ruby-forum.com/t/has-many-through-accessing-join-model-attribute-newbie/72367>\
**Category:** Rails\
**Created:** [October 16, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/has-many-through-accessing-join-model-attribute-newbie/72367 "2006-10-16T19:55:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [October 16, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/has-many-through-accessing-join-model-attribute-newbie/72367/1 "2006-10-16T19:55:30Z")

</div>

from the rails recipes i have a has\_many through “working”.  
I can list a user and what magazines he subscribes to. but I can’t  
seem to figure out how to get the other attributes from subscriptions.  
(like last\_renewed\_on)

reader\_controller.rb

def list\_reader  
@readers = Reader.find(1)  
end

* * *

list\_readers.rhtml

\<%= readers.name %\> 

\<% @readers.magazine.each do |reader|%\>

\<%= reader.title%\>\<% end %\>---------- I've tried \<%= reader.last\_renewed\_on %\> \<%= reader.subscription.last\_renewed\_on %\> \<%= subscription.last\_renewed\_on %\>

i’m obviously lost.  
i perused the has many through blog and flipped through my books and  
googled. I also have a hard time turning the “command line” examples  
into “views”

thanks  
john

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [October 16, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/has-many-through-accessing-join-model-attribute-newbie/72367/2 "2006-10-16T23:25:43Z")

</div>

John I. wrote:

> from the rails recipes i have a has\_many through “working”.  
> I can list a user and what magazines he subscribes to. but I can’t  
> seem to figure out how to get the other attributes from subscriptions.  
> (like last\_renewed\_on)
> 
> reader\_controller.rb
> 
> def list\_reader  
> @readers = Reader.find(1)  
> end
> 
> * * *
> 
> list\_readers.rhtml
> 
> \<%= readers.name %\> 
> 
> \<% @readers.magazine.each do |reader|%\>
> 
> \<%= reader.title%\>\<% end %\>---------- I've tried \<%= reader.last\_renewed\_on %\> \<%= reader.subscription.last\_renewed\_on %\> \<%= subscription.last\_renewed\_on %\>

First off, your iterator code isn’t as clear as it could be.

Is @readers a collection?

If it’s not, then it should probably read…

@reader.magazines.each do |magazine|

Since within the block you are working with magazines and not readers  
(directly).

You should be able to access the subscription model directly from both  
your reader and magazine models. If you can’t, you probably don’t have  
it setup properly.

An example from some code I wrote a few days ago…

class Ticket

has\_many :showings  
has\_many :days, :through =\> :showings

end

class Day

has\_many :showings, :dependent =\> :destroy  
has\_many :tickets, :through =\> :showings

end

class Showing

belongs\_to :day  
belongs\_to :ticket

end

I’d suggest playing around in the console to make absolutely certain  
everything works before writing views. (Actually, I’d _really_ suggest  
you write unit tests to make sure everything works (and continues to  
work as you write more code).)

Anywho, hope that helps out.

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [October 17, 2006, 1:00am UTC](https://www.ruby-forum.com/t/has-many-through-accessing-join-model-attribute-newbie/72367/3 "2006-10-17T01:00:08Z")

</div>

If you are using edge Rails, it makes it a lot easier to handle this  
kind of relationship. I would look at the DHH’s Railsconf 2006 pdf  
presentation online. It has similar examples. Another great resource  
is Peepcode screencast on RESTful Rails.

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [October 17, 2006, 8:33am UTC](https://www.ruby-forum.com/t/has-many-through-accessing-join-model-attribute-newbie/72367/4 "2006-10-17T08:33:23Z")

</div>

Since the has\_many through plugin has become part of the edge Rails,  
will it create any problem if I have the plugin installed and use the  
edge Rails at the same time?

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [October 17, 2006, 2:59pm UTC](https://www.ruby-forum.com/t/has-many-through-accessing-join-model-attribute-newbie/72367/5 "2006-10-17T14:59:01Z")

</div>

I’ll check those out. thanks
