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

can't use joins in has_one scope block? #32196

Closed
InteNs opened this issue Mar 8, 2018 · 3 comments
Closed

can't use joins in has_one scope block? #32196

InteNs opened this issue Mar 8, 2018 · 3 comments

Comments

@InteNs
Copy link

InteNs commented Mar 8, 2018

Trying to use a scope with joins() doesn't include that join in the generated sql, be it directly, via named_scopes or via default_scopes.

Below is a minimal example that reproduces this issue.
This is causing trouble because i need to scope the primary_marking based on an attribute from it's marking_code (there would be a .where(...) following the join

Steps to reproduce

class DamageIncident < ApplicationRecord
  has_one :primary_marking, -> { joins(:marking_code) }
end

class PrimaryMarking < ApplicationRecord
  belongs_to :marking_code
end

irb> DamageIncident.joins(:primary_marking)

Expected behavior

SELECT damage_incidents.*
FROM damage_incidents
  INNER JOIN primary_markings
    ON primary_markings.damage_incident_id = damage_incident.id
  INNER JOIN marking_codes
    ON primary_markings.marking_code_id = marking_codes.id

Actual behavior

SELECT damage_incidents.*
FROM damage_incidents
  INNER JOIN primary_markings
    ON primary_markings.damage_incident_id = damage_incident.id

System configuration

Rails version: Rails 5.1.4

Ruby version: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]

@kamipo
Copy link
Member

kamipo commented Mar 8, 2018

This should be fixed by #29413.
Can you try to reproduce it on master?

@rails-bot rails-bot bot added the stale label Jun 7, 2018
@rails-bot
Copy link

rails-bot bot commented Jun 7, 2018

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-2-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@InteNs
Copy link
Author

InteNs commented Jun 8, 2018

confirmed fixed on latest rails version!

@InteNs InteNs closed this as completed Jun 8, 2018
@rails-bot rails-bot bot removed the stale label Jun 8, 2018
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

2 participants