If I have a model called Newsletter with: has_many :subscribers, :through => :subscriptions and wanted to list all the subscribers, but show what level their subscriptions was (level is in subscriptions table), how would I best do that? Do I have to use my own select to get what I want or are the attributes from subscriptions available to me through the join somehow (I am suspecting not)? Thanks in advance Chris
On May 23, 2006, at 04:43 PM, Chris Bruce wrote:> If I have a model called Newsletter with: > > has_many :subscribers, :through => :subscriptions > > and wanted to list all the subscribers, but show what level their > subscriptions was (level is in subscriptions table), how would I > best do > that? > > Do I have to use my own select to get what I want or are the > attributes > from subscriptions available to me through the join somehow (I am > suspecting not)?Hmm... not sure I see that there''s much of a problem here, since you have a standard has_many association between newsletters and subscriptions. So you can simply do: @subscriber_subscriptions = @newsletter.subscriptions.find (:all, :include => :subscribers) This is also what you would do if you wanted a custom sort order on the returned subscriptions (or subscribers). I suppose there might be a fancier way of doing this, but I''ll let Josh Susser handle that. :) -Brian
Josh Susser
2006-May-24 05:01 UTC
[Rails] Re: has_many :through extra domain model question
Brian Hughes wrote:> On May 23, 2006, at 04:43 PM, Chris Bruce wrote: >> attributes >> from subscriptions available to me through the join somehow (I am >> suspecting not)? > > Hmm... not sure I see that there''s much of a problem here, since you > have a standard has_many association between newsletters and > subscriptions. So you can simply do: > > @subscriber_subscriptions = @newsletter.subscriptions.find > (:all, :include => :subscribers) > > This is also what you would do if you wanted a custom sort order on > the returned subscriptions (or subscribers). I suppose there might be > a fancier way of doing this, but I''ll let Josh Susser handle that. :)You rang? :-) When doing a query with a has_many :through, the attributes for the join model are in fact available, and you can use them in :select and :conditions options, so you can use a :select to include the level attribute in the result of the query. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Possibly Parallel Threads
- has_many :through and foreign key parameters
- More than one has_many :through association between the same 2 models
- Handling a relationship between users and newsletter subcriptions.
- Slightly OT : newsletters, mail formatting and netiquette
- has_many :through with has_many/has_many join models