-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
with_any_role(:role1, :role2) on User Object returns Array instead of ActiveRecord::Relation #292
Comments
Thanks for reporting this! We'll get this fixed up for the next release. |
The same is true for |
You can combine ActiveRecord relations using |
Has there been any progress on this? I was really hopeful that this method would do exactly what I wanted, but then I saw it was an array, which prevented me from chaining it with other queries. |
Alright, I figured out a way to fix this method. I've got a PR open now (#441) |
workaround # user.rb
scope :with_any_roles, ->(names) do
joins(:roles).where("roles.name IN (?)", names)
end
# using
User = User.with_any_roles([:admin, :moderator]) |
@nhattan If user has multiple roles, then with that query you will get duplicate rows. Of course |
Still not released? |
Thanks @kakoni |
Any update on this? |
WIP here #441 TL;DR: Works with Rails 4.2 but not (yet) on Rails 5 |
@kakoni just be aware that the workaround does not work when there is a column with type |
@milani - I'm using your code right now and it works like a charm. Still waiting for this feature in a Gem |
It would be best if all role queries returned an AR, not an Array. |
6 years 🥇 |
with_any_role(:role1, :role2) on User Object returns Array instead of ActiveRecord::Relation
This is a rather tiny issue, but it is rather surprising, since with_role already correctly returns a Relation Object which you can easily chain.
The problem showed up when I chained other Database scopes, and the temporary solution is to do the with_any_role at the end of the chain. This conflicts with other scopes like pagination, and of course the Array cannot be evaluated lazily.
The text was updated successfully, but these errors were encountered: