Forum: Ruby on Rails has_many :through extra domain model question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B29105598b6464efc13b04cb445efa85?d=identicon&s=25 Chris Bruce (Guest)
on 2006-05-23 22:45
(Received via mailing list)
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-05-23 23:20
(Received via mailing list)
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. :)

-Brian
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-05-24 07:00
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
This topic is locked and can not be replied to.