How do I set up a has_one through association through a has_many through association?

Go To StackoverFlow.com

2

I have a User model, a Membership model, and a Club model. I have set up the following associations:

Club.rb

has_many :memberships
has_many :members, through: :memberships

Membership.rb

belongs_to :user
belongs_to :club

What I want now is to have a has_one :organizer on Club that retrieves a single User record based on the member with the organizer attribute set to true in the join model.

How do I set up this has_one association? I've tried going through the memberships association, but an exception is raised that the memberships is a collection and needs to be singular.

2012-04-04 02:24
by Eric M.


2

I think I just figured it out...but in case someone else needs to know..

has_one :membership, conditions: ['memberships.organizer = ?', true]
has_one :organizer, through: :membership, source: :user
2012-04-04 02:27
by Eric M.
Ads