【发布时间】:2017-02-15 06:46:13
【问题描述】:
我正在尝试获取Rolify's Finder Methods 之一(第 3 号),但它总是返回一个空数组。
用户模型:
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).participants 和User.with_any_role :admin, { name: :moderator, resource: Forum.find(3) },都返回空数组,例如:
2.4.0 :027 > Forum.find(3).participants
Forum Load (21.9ms) SELECT ...
User Load (28.7ms) SELECT ...
=> []
这里有什么我遗漏的吗?
谢谢!
【问题讨论】:
-
你添加任何角色了吗?
-
@bananaappletw 我肯定有。
-
您输入什么命令来添加角色?也许我可以帮你调试一下。
-
按照您的描述。我认为您将角色添加到论坛课程。不是实例。输入此
User.with_any_roles :admin, { :name => :moderator, :resource => Forum }。让我们看看结果。 -
添加了添加角色和测试问题的命令。
标签: ruby-on-rails activerecord rolify