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

Bad query generation in very specific case #598

Open
jpatters opened this issue Jan 20, 2024 · 0 comments
Open

Bad query generation in very specific case #598

jpatters opened this issue Jan 20, 2024 · 0 comments

Comments

@jpatters
Copy link

This is a bit of a weird one and only surfaced because of a bug in another gem. I don't believe it should be an issue under normal circumstances.

I ran into an issue in another gem where, during runtime, it was causing a rails model to become a subclass of itself. Because of that this method https://github.com/RolifyCommunity/rolify/blob/master/lib/rolify/adapters/base.rb#L27-L29 was returning the same class multiple times (ie. ['Organization', 'Organization']).

The above issue caused bad queries to be generated here: https://github.com/RolifyCommunity/rolify/blob/master/lib/rolify/adapters/active_record/resource_adapter.rb#L14-L19. This resulted in ...INNER JOIN roles ON roles.resource_type IN ('Organization''Organization') AND... to appear in the query; which of course is invalid.

Again, this was only surfaced because of a bug in another gem. I thought I would bring it up here in case it is desired that it be fixed in rolify as well.

The code below would resolve the issue in a defensive manor.

def relation_types_for(relation)
  relation.descendants.map(&:to_s).push(relation.to_s).uniq
end

Alternatively, this is the problematic line https://github.com/RolifyCommunity/rolify/blob/master/lib/rolify/adapters/active_record/resource_adapter.rb#L17. It could be changed to use the array index instead of the value.

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

1 participant