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

add_role could be better optimized #558

Open
MatthewSuttles opened this issue Apr 19, 2021 · 2 comments
Open

add_role could be better optimized #558

MatthewSuttles opened this issue Apr 19, 2021 · 2 comments

Comments

@MatthewSuttles
Copy link

We currently use Rolify in our application. Adding a role for a user was getting very slow and would break for most of our clients.

Doing some investigation I found the add method was doing this.

def add(relation, role) relation.roles << role unless relation.roles.include?(role) end

The includes seems to be loading ever record.
Cursor_and_Terminal_—ruby_bin_rails_c—_143×56

If someone could verify they are seeing the same thing I would really appreciate it. Currently using Rails 5.2.2

@thomas-mcdonald
Copy link
Member

This is unexpected - can you run this with something that will show you location those queries are being generated from? Newer Rails should do it out of the box or something like https://github.com/brunofacca/active-record-query-trace will help

@trein-homeroom
Copy link

I've hit this as well. The location is as mentioned above, in the add method, the roles.includes call. It causes all roles to load, rather than having the DB do the work. It should be roles.exists instead.

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

3 participants