Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #370
Summary
Querying with joins can sometimes produce invalid sql. The two cases that this PR handles are:
Description
These situations are rather easy to get to when users attempt to make composable methods that limit the scope of queries. Say an app has the concept of a
Business
and aUser
and if the user belongs to a business they are considered anEmployee
of that business. If the app allowed users to access the api, it might make sense to limit their API access to businesses they are an employee of. Rather than having to remember how to scope their access correctly every time you have a new query, you could make helper methods like (granted this is convoluted, I'm sure there are more realistic ways to get in this situation):(Those methods would require that
Business
has a has many through association toUser
throughEmployee
)The problem arises when you want to provide an endpoint that returns all the employees for the user's current business. The action would do something like:
You end up with an error:
table name "employees" specified more than once (PQ::PQError)
because the BusinessQuery adds a join to limit by the user id and the main query is on the employees table.Note
I'm sure there are situations this doesn't handle and this might even cause issues so PLEASE poke holes in this change.