2016-04-24 19 views
0

ユーザーの役割に応じて異なる種類のWikiをインデックスビューに表示します。 adminstandard/guestユーザーの方針は、それほどうまくいくはずですが、プレミアムユーザーとコラボレーションについては少し面倒です。私のアプリでは、コラボレーターをプライベートWikisに追加する機能があります。だからプレミアムユーザは私のプライベートウィキ、公開ウィキ、プライベートウィキを見ることができるはずですが、プライベートウィキはどこにいても私のために現れません。私のポリシーやモデル団体と関係がありますか?私を助けてくださいPundit Policy:Wikisが共同編集者に公開されていません

ウィキ#インデックス

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 
.... 

ウィキモデル

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

を助け

希望を説明してきたように、私は協力者がテーブルに参加しWikiCollaboratorとして、それを再生すると、より多くの意味を行い、その後、実装してドロップすると思う です。あなたが標準ユーザーとしてログインしているときに働いているため、実際にあなたが使用している共同編集者にwikiを割り当てていないと思われます。コードは失敗するようには見えません – joewoodward

+0

私は使用したコンソールでそれをテストしました: 'last = Wiki.last、last.collaborators'、 '#]> ' –

答えて

1

ああ、私はこの問題を参照してください。あなたの結合テーブルの形式が間違っています。これはおそらく、同じクラスに対してbelongs_toとhas_many throughの両方を使用しているので、名前が混乱していたためです。すなわち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協力者を得るためにwiki.users(複数に注意してください)を呼び出すことができます。 あなたは依然として「belongs_to:collaborators」という行を修正する必要があります。私はこれがコンソールに手動でコードをテストしてみてください

関連する問題