Eric M.
Eric M.

Reputation: 5549

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

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.

Upvotes: 2

Views: 107

Answers (1)

Eric M.
Eric M.

Reputation: 5549

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

Upvotes: 2

Related Questions