Pundit 从另一个模型授权
Pundit Authorize From Another Model
我需要根据供应商收到的邀请授权一个项目。供应商有一个 "user_id" 字段。
project.rb
has_many :invites
has_many :suppliers, :through => :invites
project_policy.rb
class ProjectPolicy < ApplicationPolicy
attr_reader :user, :project
def initialize(user, project)
@user = user
@project = project
end
def show?
##need help on the next line##
if project.joins(:invites).joins(:suppliers).pluck("suppliers.user_id") == user.id
return true
else
return false
end
end
end
如何根据供应商 table 中的 user_id
只显示合适的项目?如果这需要在范围内,那么如何检查范围内的 suppliers.user_id
?
How can I show only the appropriate projects based on the user_id
in the suppliers table?
The has_many :through Association
Invites.rb
belongs_to :supplier
belongs_to :project
项目模型有多个供应商
Project.rb
has_many :invites
has_many :suppliers, :through => :invites
供应商模型有很多项目
Supplier.rb
has_many :invites
has_many :projects, :through => :invites
找到供应商并使用has_many :projects, :through => :invites
Supplier.find_by(user_id: user.id).projects
我需要根据供应商收到的邀请授权一个项目。供应商有一个 "user_id" 字段。
project.rb
has_many :invites
has_many :suppliers, :through => :invites
project_policy.rb
class ProjectPolicy < ApplicationPolicy
attr_reader :user, :project
def initialize(user, project)
@user = user
@project = project
end
def show?
##need help on the next line##
if project.joins(:invites).joins(:suppliers).pluck("suppliers.user_id") == user.id
return true
else
return false
end
end
end
如何根据供应商 table 中的 user_id
只显示合适的项目?如果这需要在范围内,那么如何检查范围内的 suppliers.user_id
?
How can I show only the appropriate projects based on the
user_id
in the suppliers table?
The has_many :through Association
Invites.rb
belongs_to :supplier
belongs_to :project
项目模型有多个供应商
Project.rb
has_many :invites
has_many :suppliers, :through => :invites
供应商模型有很多项目
Supplier.rb
has_many :invites
has_many :projects, :through => :invites
找到供应商并使用has_many :projects, :through => :invites
Supplier.find_by(user_id: user.id).projects