John Ivanoff
2006-Oct-16 17:50 UTC
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 <dl> <dt><%= readers.name %> </dt> <% @readers.magazine.each do |reader|%> <dd><%= reader.title%></dd> <% end %> </dl> ---------- 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Daniel Waite
2006-Oct-16 21:25 UTC
Re: has many :through, accessing join model attribute (newbi
John Ivanoff 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 > <dl> > <dt><%= readers.name %> </dt> > > <% @readers.magazine.each do |reader|%> > <dd><%= reader.title%></dd> > <% end %> > </dl> > ---------- > 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Oct-16 22:58 UTC
Re: has many :through, accessing join model attribute (newbie)
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. On 10/16/06, John Ivanoff <john.ivanoff-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Oct-17 06:31 UTC
Re: has many :through, accessing join model attribute (newbi
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''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).)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
John Ivanoff
2006-Oct-17 12:56 UTC
Re: has many :through, accessing join model attribute (newbie)
I''ll check those out. thanks On 10/16/06, Bala Paranj <bcparanj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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. > > On 10/16/06, John Ivanoff <john.ivanoff-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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) > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---