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

Fix scope overriding join relation query #615

Merged
merged 1 commit into from
Dec 19, 2024

Conversation

henriquetatagiba
Copy link
Contributor

@henriquetatagiba henriquetatagiba commented Aug 28, 2024

Adding scopes after the join relation removes the join query from the generated SQL.

Rearrange code to match rails 7.0 method

Example:

class User < ApplicationRecord
  has_many :features, -> { enabled }
end

class Feature < ApplicationRecord
  scope :enabled, -> { where.not(user_id: nil).where(enabled: true) }
end

# User.joins(:features).to_sql
# INNER JOIN query won't include `users.id = features.user_id` but only `features.user_id IS NOT NULL`
# when the expected is both

@henriquetatagiba henriquetatagiba changed the title Fix scope removes join query Fix scope overriding join relation query Aug 28, 2024
@cfis cfis merged commit bb1b2fc into composite-primary-keys:master Dec 19, 2024
17 of 20 checks passed
@cfis
Copy link
Contributor

cfis commented Dec 19, 2024

Sorry for not responding to this earlier. Thanks for the fix!

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

Successfully merging this pull request may close these issues.

2 participants