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

has_one association works stand-alone but does not, when used in scope #22407

Closed
andreydeineko opened this issue Nov 25, 2015 · 6 comments
Closed

Comments

@andreydeineko
Copy link

class Company::ExpositionTarget::DiscretionaryNote < ActiveRecord::Base
  belongs_to :company_exposition_target,
    class_name: ::Company::ExpositionTarget
 end

class Company::ExpositionTarget < ActiveRecord::Base
  has_many :discretionary_notes,
    class_name: ::Company::ExpositionTarget::DiscretionaryNote,
    foreign_key: :company_exposition_target_id

    has_one :last_discretionary_note,
      ->(exposition_target) { where(company_exposition_target: exposition_target).order(created_at: :desc).limit(1) },
      class_name: ::Company::ExpositionTarget::DiscretionaryNote,
      foreign_key: :company_exposition_target_id
    # just `order(created_at: :desc).limit(1)` - also works stand-alone, but not in scope
end

Each of defined associations work 100%. The one we are interested in is has_one :last_discretionary_note.

Company::ExpositionTarget.find(102).last_discretionary_note

Generated SQL:

SELECT  company_exposition_targets.*
FROM company_exposition_targets
WHERE company_exposition_targets.id = 102
LIMIT 1

SELECT company_exposition_target_discretionary_notes.* 
FROM company_exposition_target_discretionary_notes 
WHERE company_exposition_target_discretionary_notes.company_exposition_target_id = 102 
 AND company_exposition_target_discretionary_notes.company_exposition_target_id = 102
LIMIT 1

Which returns the expected result - last discretionary_note.

What I initially need:

sort exposition_targets by the last discretionary_note.attribute

To do the sorting I want to create a scope:

scope :discretionary_notes_available, ->(true_false) {
  joins(:last_discretionary_note)# more data selection based on true_false
}

Now the problem:

Company::ExpositionTarget.joins(:last_discretionary_note) returns nothing.

Generated SQL:

SELECT company_exposition_targets.*
FROM company_exposition_targets
INNER JOIN company_exposition_target_discretionary_notes
ON company_exposition_target_discretionary_notes.company_exposition_target_id = company_exposition_targets.id 
AND company_exposition_target_discretionary_notes.company_exposition_target_id = NULL

This query is wrong and does not take into account the definition of has_one: last_discretionary_note.

QUESTION:

Why does AR not treating the has_one association correctly in scope? And, what is more important - where is my mistake and how do I achieve what I need?

Again, schematically I need the following:

scope :discretionary_notes_available, ->(true_false) {
  select all company_exposition_targets where company_exposition_targets.discretionary_notes.last.allow_participant_to_download_pdf = true_false
}
@timbreitkreutz
Copy link

This looks a bit like another issue I've been trying to understand: #9157

It seems like there are some chicken and egg problems with regard to setting up associations in time for assignments lambdas, callbacks, and also the association lambdas like in this ticket.

@andreydeineko
Copy link
Author

@timbreitkreutz so do you think this issue has a chance to be examined by someone from Rails team or is it rather a rare case which is not worth it?

@maclover7
Copy link
Contributor

Can you create a standard reproduction script that uses one of the templates here?

@timbreitkreutz
Copy link

Sorry @andreydeineko I got absorbed in another project so I haven't any chance to do more research...

@rails-bot
Copy link

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

@rails-bot
Copy link

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 5-0-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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants