2017-02-15 75 views
1

我想要得到Rolify's Finder Methods去(3)之一,但它總是返回一個空的數組。Rolify finder方法返回空數組 - Rails

用戶模式:

class User < ActiveRecord::Base 
    rolify 

    has_many :forums, dependent: :destroy 
end 

論壇模式:

class Forum < ActiveRecord::Base 
    resourcify 

    belongs_to :user 

    def participants 
    # Find all users with any role in the forum 
    User.with_any_role :admin, { name: :moderator, resource: self } 
    end 
end 

添加角色:

user.add_role :admin, Forum.find(3) 

個測試角色:

2.4.0 :043 > user.has_role? :admin, Forum.find(3) 
    Role Load (21.9ms) SELECT ... 
=> true 

我試圖從控制檯運行Forum.find(3).participantsUser.with_any_role :admin, { name: :moderator, resource: Forum.find(3) },並且都返回空數組,e.g:

2.4.0 :027 > Forum.find(3).participants 
    Forum Load (21.9ms) SELECT ... 
    User Load (28.7ms) SELECT ... 
=> [] 

有我丟失的東西嗎?

謝謝!

+0

你添加任何作用了嗎? – bananaappletw

+0

@bananaappletw我當然有。 – slehmann36

+0

你輸入什麼命令來添加角色? 也許我可以幫你調試它。 – bananaappletw

回答

1

通過跟蹤代碼here 您可以將參數作爲字符串或散列傳遞。

User.with_any_role({name: :admin, resource: Forum.find(3)},{name: :moderator, resource: Forum.find(3)}) 

將搜索所有用戶具有admin或主持人角色的論壇實例,該實例ID是3