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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

henriquetatagiba
Copy link

@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
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.

1 participant