将命名范围与has_many一起使用

时间:2011-08-18 03:16:56

标签: ruby-on-rails ruby ruby-on-rails-3 has-many-through

我有以下型号:

class Board < ActiveRecord::Base

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

这允许我在我的控制器和视图中执行以下操作

@board.users.manager

有没有办法使用named_scope来获取电路板的管理器,如下所示:

@board.manager

1 个答案:

答案 0 :(得分:2)

以下是一个示例解决方案:

class Board < ActiveRecord::Base

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

  delegate :manager, :to => :users

end