鉴于以下内容:
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获取输出。感谢
答案 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}}