鉴于以下用户<>项目<>权限<>角色模型,如何获得项目的团队成员?

时间:2010-09-17 22:26:14

标签: ruby-on-rails activerecord ruby-on-rails-3

鉴于以下内容:

class User < AR::B 
  has_many :permissions 
  has_many :projects, :through => :permissions 
end 
class Project < AR::B 
  has_many :permissions 
  has_many :users, :through => :permissions 
end 
class Role < AR::B 
  has_many :permissions 
end 
class Permission < AR::B 
  belongs_to :user 
  belongs_to :project 
  belongs_to :role 
end

我有兴趣在项目模型(project.rb)中创建一个SCOPE,它返回所有项目的成员(基于权限表&lt;&gt; users&amp; role tables。

期望的输出: user.name,role.name

这是我在模型中的范围,它没有返回所需的范围 输出:

class Project < ActiveRecord::Base 
has_many :permissions 
has_many :users, :through => :permissions 
 #Playing with Scopes 
  scope :teammembers, 
  Project.permissions.joins(:users, :roles) 
end 

试图找出如何使用ActiveRecord获取输出。感谢

1 个答案:

答案 0 :(得分:0)

Class Project < AR:B
...
    def members
        User.joins(:permissions => :project).where(:permissions => {:projects => self})
    end
...

此处self可以替换为某个ID,如果您要将self.id更改为:projects

,则:project_id可以替换为{{1}}