【发布时间】:2014-12-04 16:03:03
【问题描述】:
我通过一个连接表将用户与给定的公司关联起来,因为我需要能够让每个公司都有大量用户,反之亦然。
class User
has_many :firm_connections, dependent: :destroy
has_many :firms, through: :firm_connections
end
class FirmConnection
belongs_to :user
belongs_to :firm
end
class Firm
has_many :firm_connections
has_many :users, through: :firm_connections
end
我的问题是,当用户点击公司的索引页面时,我如何将其范围限定为仅显示与这些用户相关联的内容?
class FirmPolicy < ApplicationPolicy
class Scope < Scope
def resolve
if user.admin?
scope.all
else
scope.where #only the firms associated with that user
end
end
end
我是否需要在公司级别创建一个接受@user 的范围?或者我可以直接内联吗?我可以一起破解一些东西,但还没有把我的头绕到专家面前,所以任何方向都将不胜感激!
像这样:
def self.associated_with(user)
all.select { |m| m.users.include?(user) }
end
【问题讨论】:
标签: mysql ruby-on-rails ruby join pundit