2016-04-24 70 views
0

我希望根據用戶的角色在索引視圖上顯示不同類型的維基。 adminstandard/guest用戶的政策應該像它應該的那樣工作,但是當涉及到高級用戶和協作時,它會變得有點混亂。在我的應用程序中,我可以添加合作者到私人Wikis。因此,一個高級用戶我應該能夠看到我的私人維基,公共維基和私人維基,但是在合作者沒有爲我顯示的私人維基上。它可能與我的政策或我的模型關聯有關嗎?請幫我Pundit政策:Wiki未向合作伙伴展示

維基#指數

def index 
    @wikis = Kaminari.paginate_array(policy_scope(Wiki)).page(params[:page]).per(10) 
    end 

用戶模型

class User < ActiveRecord::Base 
    has_many :wikis 
    has_many :collaborators 
    belongs_to :collaborators 
.... 

wiki模式

class Wiki < ActiveRecord::Base 
     belongs_to :user 
     has_many :collaborators 
     has_many :users, through: :collaborators 
.... 

合作者模型

class Collaborator < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :wiki 
end 

Wiki_policy

class Scope 
    attr_reader :user, :scope 

    def initialize(user, scope) 
     @user = user 
     @scope = scope 
    end 

    def resolve 
     wikis = [] 
     if user.role == 'admin' 
     wikis = scope.all # if the user is an admin, show them all the wikis 
     elsif user.role == 'premium' 
     all_wikis = scope.all 
     all_wikis.each do |wiki| 
      if wiki.private == false || wiki.owner == user || wiki.collaborators.include?(user) 
      wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on 
      end 
     end 
     else # this is the lowly standard user 
     all_wikis = scope.all 
     wikis = [] 
     all_wikis.each do |wiki| 
      if wiki.private == false || wiki.collaborators.include?(user) 
      wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on 
      end 
     end 
     end 
     wikis # return the wikis array we've built up 
    end 
    end 

,當我去到控制檯

last = Wiki.last 
last.collaborators 

我得到這個:

=> #<ActiveRecord::Associations::CollectionProxy [#<Collaborator id: 7, user_id: 8, wiki_id: 104, created_at: "2016-04-24 08:07:20", updated_at: "2016-04-24 08:07:20">]> 
+0

嘗試在控制檯手動測試代碼。我懷疑你實際上沒有將wiki分配給你使用的協作者,因爲它以標準用戶身份登錄時正在工作。代碼看起來不像應該失敗 – joewoodward

+0

我在控制檯上測試了它:'last = Wiki.last,last.collaborators'並得到了'#]>' –

回答

1

啊,我看這個問題。您的連接表格格式錯誤,這可能是因爲您正在同一個類中同時使用belongs_to和has_many,所以您對命名感到困惑。即belongs_to的:用戶的has_many:用戶

您需要通過定義趁CLASS_NAME在

Wiki 
    # the owner 
    belongs_to :user 

    # the collaborators join table 
    has_many :wiki_collaborators 

    # this is the object you will call from wiki.collaborators 
    has_many :collaborators, through: :wiki_collaborators, class_name: 'User' 
end 

User 
    # the wikis owned by this user 
    has_many :wikis 

    # the join table 
    has_many :wiki_collaborators 

    # this is the object you can call from user.wiki_collaborations or something else that maybe fits better 
    has_many :wiki_collaborations, through: :wiki_collaborators, class_name: 'Wiki' 
end 

WikiCollaborator 
    belongs_to :user 
    belongs_to :wiki 
end 

您的問題是,當你在呼喚你wiki.collaborator實際上返回連接模型而不是用戶模型。

你實際上可以調用wiki.users(注意複數)來獲得當前代碼中的wiki協作者。 儘管如此,您仍然需要修復用戶belongs_to:協作者行。 我認爲下降的合作者加入表,並將其作爲再生WikiCollaborator會更有意義,然後實現如我所描述

希望這有助於