0
在我構建的應用程序中,用戶可以將「供稿者」添加到他們的帳戶中,以幫助他們爲他們填寫其個人檔案的某些方面。我使用cancan
來幫助定義功能,但由於某種原因,爲貢獻者返回的查詢以及他們可以訪問的用戶也無法正確返回。CanCan使用accessible_by不會生成正確的SQL語句
這是我最大的能力文件:
module Abilities
class UserAbility < Ability
def initialize(user)
super(user)
if user.athlete_contributor?
can :update, Athlete, user: { id: user.managed_athlete_ids }
end
end
end
end
用戶模型:
has_many :contributorships
has_many :managed_athletes, through: :contributorships, source: :athlete
投稿者型號:
class Contributorship < ActiveRecord::Base
belongs_to :user
belongs_to :athlete
belongs_to :contributor, :class_name => "User"
belongs_to :user_type
attr_accessible :user_id, :user_type_id, :athlete_id
validates :user_type, presence: true
end
貢獻者儀表板控制器:
class ContributorController < ApplicationController
before_filter :authenticate_user!
def index
authorize! :update, Athlete
@athletes = Athlete.accessible_by(current_ability)
end
end
的@athletes
實例變量返回的
SELECT "users".* FROM "users" WHERE "users"."type" IN ('Athlete') AND ('t'='f')
不知道我做錯了查詢,但該查詢看起來搗毀,LOL。先謝謝您的幫助!