Skip to content
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

Named Scopes and Return All Instances of Role not working with subclasses #9

Open
jeff-savin opened this issue Jul 22, 2015 · 3 comments

Comments

@jeff-savin
Copy link

If I have a subclass of User, say Admin, with royce_roles [:add, :edit, :delete, :crud], the following gives me empty collections:

@admin = Admin.first
@admin.add_role :crud
Admin.cruds  # returns empty

crud_role = Royce::Role.find_by(name: 'crud')
crud_role.admins.all  # returns empty

Looking in SQL, royce_connector shows User as roleable_type instead of Admin which is what is causing the empty sets.

@jeff-savin
Copy link
Author

To expand a little on the above, I noticed doing a user.role_list builds this SQL query:

SELECT `royce_role`.`name` FROM `royce_role` INNER JOIN `royce_connector` ON `royce_role`.`id` = `royce_connector`.`role_id` WHERE `royce_connector`.`roleable_id` = 641702 AND `royce_connector`.`roleable_type` = 'User'

and the crud_role.admins.all builds this SQL query

SELECT `users`.* FROM `users` INNER JOIN `royce_connector` ON `users`.`id` = `royce_connector`.`roleable_id` WHERE `users`.`type` IN ('Admin') AND `royce_connector`.`role_id` = 1 AND `royce_connector`.`roleable_type` = 'Admin'

As mentioned in above note, the roleable_type here is stored as 'User' allowing the role_list to work, but not the named scopes as mentioned above.

@jeff-savin
Copy link
Author

I've forked this and made changes, which seems to work. I'll submit a pull request if you're interested. Thanks.

@grzlus
Copy link
Contributor

grzlus commented Aug 30, 2015

@jeff-savin It's always good to submit pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants