2017-02-15 72 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