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

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

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.

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.

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?

I’ll check those out. thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs