chell
chell

Reputation: 7866

Use named scope with has_many_through

I have the following model:

class Board < ActiveRecord::Base

has_many   :users, :through => :participants do
                 def manager
                     where("participants.role = ?", "Manager").first
                 end
               end

This allows me to do the following in my controller and views

@board.users.manager

Is there a way to use a named_scope to be able to get the manager for a board as follows:

@board.manager

Upvotes: 1

Views: 73

Answers (1)

Maur&#237;cio Linhares
Maur&#237;cio Linhares

Reputation: 40333

Here's a sample solution:

class Board < ActiveRecord::Base

  has_many   :users, :through => :participants do
    def manager
      where("participants.role = ?", "Manager").first
    end
  end

  delegate :manager, :to => :users

end

Upvotes: 2

Related Questions