【问题标题】:Rolify finder method returning empty array - RailsRolify finder 方法返回空数组 - Rails
【发布时间】: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).participantsUser.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 =&gt; :moderator, :resource =&gt; Forum }。让我们看看结果。
  • 添加了添加角色和测试问题的命令。

标签: ruby-on-rails activerecord rolify


【解决方案1】:

通过追踪码here 您可以将参数作为字符串或哈希传递。

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

将搜索 ID 为 3 的论坛实例的所有具有管理员或版主角色的用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多