Rolify finder 方法返回空数组 - Rails
Rolify finder method returning empty array - Rails
我正在尝试让 Rolify's Finder Methods 中的一个(第 3 位)运行,但它总是 return 一个空数组。
用户模型:
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) }
,并且两个 return 都是空数组,例如:
2.4.0 :027 > Forum.find(3).participants
Forum Load (21.9ms) SELECT ...
User Load (28.7ms) SELECT ...
=> []
这里有我遗漏的东西吗?
谢谢!
通过跟踪代码here
您可以将参数作为字符串或哈希传递。
User.with_any_role({name: :admin, resource: Forum.find(3)},{name: :moderator, resource: Forum.find(3)})
将搜索 ID 为 3
.
的所有具有论坛实例管理员或版主角色的用户
我正在尝试让 Rolify's Finder Methods 中的一个(第 3 位)运行,但它总是 return 一个空数组。
用户模型:
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) }
,并且两个 return 都是空数组,例如:
2.4.0 :027 > Forum.find(3).participants
Forum Load (21.9ms) SELECT ...
User Load (28.7ms) SELECT ...
=> []
这里有我遗漏的东西吗?
谢谢!
通过跟踪代码here 您可以将参数作为字符串或哈希传递。
User.with_any_role({name: :admin, resource: Forum.find(3)},{name: :moderator, resource: Forum.find(3)})
将搜索 ID 为 3
.