Has_many :through extra domain model question


#1

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


#2

On May 23, 2006, at 04:43 PM, Chris B. 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 S. handle that. :slight_smile:

-Brian


#3

Brian H. wrote:

On May 23, 2006, at 04:43 PM, Chris B. 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 S. handle that. :slight_smile:

You rang? :slight_smile:

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 S.
http://blog.hasmanythrough.com