第一次在这里发帖,所以我希望它是有道理的,但是我在这方面已经旋转了太久了。首先是一点背景。我正在构建一个具有 3 个模型的 Wiki 应用程序:用户、Wiki 和协作(连接表)。
我正在使用 Devise 和 Pundit,并且有 4 类用户应该根据他们的状态查看不同的 wiki 子集。
这是规则:
- 公共用户(未登录)- 只应查看(无编辑)公共 wikis (:hide => false)
- Authenticated User(:role => "standard") - 只能查看、编辑和删除公共 wiki。
- 高级用户 (:role => "premium") - 查看、编辑、删除公共 wiki 以及创建私人 wiki 的能力 (:hide => true) 并将协作者添加到他们的私人 wiki 中,从而赋予他们对该 wiki 的编辑权限。
- 管理员 (:role => "admin") 完全控制所有记录。
因此,我的 Wiki 索引视图在 Policy 范围(检查用户状态)中有一个冗长的 scope.joins 条件,以便根据他们的角色为 current_user 提供一个 wiki 列表的子集。
它不断吐出这个错误:
Started POST "/__better_errors/59802a57d82fd17e/variables" for 127.0.0.1 at 2014-12-02 09:05:41 -0800
Wiki Load (0.4ms) SELECT "wikis".* FROM "wikis" INNER JOIN "collaborations" ON "collaborations"."wiki_id" = "wikis"."id" WHERE (hide = 'f' or user_id = 2 or collaborations.user_id = 2)
SQLite3::SQLException: ambiguous column name: user_id: SELECT "wikis".* FROM "wikis" INNER JOIN "collaborations" ON "collaborations"."wiki_id" = "wikis"."id" WHERE (hide = 'f' or user_id = 2 or collaborations.user_id = 2)
这是政策
class WikiPolicy < ApplicationPolicy
# What collections a user can see users `.where`
class Scope < Scope
def resolve
if user && user.role == 'admin'
scope.all
elsif user
scope.joins(:collaborations).where("hide = :hide or user_id = :owner_id or collaborations.user_id = :collaborator_id", {hide: false, owner_id: user.id, collaborator_id: user.id})
else
scope.where hide: false
end
end
end
#Policies are boolean logic true or false to determine if a user has access to a controller action.
def update?
(user && user.role == 'admin') || (user && record.users.pluck(:id).include?(user.id)) || (user && user.id == record.owner.id )
end
def show?
(user.present? && user.admin?) or not record.hide?
end
def premium?
user.admin? or user.premium?
end
def edit?
end
end
我的模型
class Wiki < ActiveRecord::Base
belongs_to :user
has_many :collaborations, dependent: :destroy
has_many :users, through: :collaborations
def owner
user
end
def collaborators
users
end
validates :title, length: { minimum: 5 }, presence: true
validates :body, length: { minimum: 20 }, presence: true
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :wikis
has_many :collaborations
has_many :cowikis, through: :collaborations, source: :wiki
after_initialize :init
def admin?
role == 'admin'
end
def premium?
role == 'premium'
end
def standard?
role == 'standard'
end
private
def init
if self.new_record? && self.role.nil?
self.role = 'standard'
end
end
end
class Collaboration < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
end
维基控制器
class WikisController < ApplicationController
def index
@wikis = policy_scope(Wiki)
# authorize @wikis
end
def show
@wiki = Wiki.find(params[:id])
authorize @wiki
end
def new
@wiki = Wiki.new
authorize @wiki
end
def create
@wiki = Wiki.new(wiki_params)
authorize @wiki
if @wiki.save
flash[:notice] = "Post was saved."
redirect_to @wiki
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def edit
@user = current_user
@users = User.all
@wiki = Wiki.find(params[:id])
authorize @wiki
end
def update
@wiki = Wiki.find(params[:id])
authorize @wiki
@wiki.collaborators = params[:wiki][:user_ids]
if @wiki.update_attributes(wiki_params)
flash[:notice] = "Post was updated."
redirect_to @wiki
else
flash[:error] = "There was an error saving the post. Please try again."
render :edit
end
end
def destroy
@wiki = Wiki.find(params[:id])
authorize @wiki
title = @wiki.title
if @wiki.destroy
flash[:notice] = "\"#{title}\" was deleted successfully."
redirect_to wikis_path
else
flash[:error] = "There was an error deleting the wiki."
render :show
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :hide)
end
end
我希望你不会被代码淹没,但我想提供尽可能多的信息。
谢谢你的帮助!
哦,这很可能是有问题的代码,但我将其他所有内容都包含在上下文中。
scope.joins(:collaborations).where("hide = :hide or user_id = :owner_id or collaborations.user_id = :collaborator_id", {hide: false, owner_id: user.id, collaborator_id: user.id})