Search code examples
ruby-on-railsactiverecordrolify

Rolify finder method returning empty array - Rails


I'm trying to get one of Rolify's Finder Methods going (no. 3), but it is always returning an empty array.

User model:

class User < ActiveRecord::Base
  rolify

  has_many :forums, dependent: :destroy
end

Forum model:

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

Adding roles:

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

Testing roles:

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

I have tried running Forum.find(3).participants and User.with_any_role :admin, { name: :moderator, resource: Forum.find(3) } from the console, and both return empty arrays, e.g:

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

Is there something I am missing here?

Thanks!


Solution

  • By tracing code here you can pass argument as string or hash.

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

    Will search all the users has either admin or moderator role for Forum instance which id is 3.