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

Querying association with unscope(:where) from instance removes conditions #39275

Closed
dvandersluis opened this issue May 13, 2020 · 6 comments
Closed

Comments

@dvandersluis
Copy link
Contributor

dvandersluis commented May 13, 2020

Related to #38811. Trying to access an association of an AR instance where the association unscopes where no longer raises an exception, but still removes association conditions from the query.

Steps to reproduce

class Post < ActiveRecord::Base
  has_many :comments, -> { unscope(:where) }
end

class Comment < ActiveRecord::Base
  belongs_to :post

  default_scope { where(locale: :en) }
end

post = Post.create!
post.comments << Comment.create!(locale: nil)
post.comments.to_sql #=> SELECT "comments".* FROM "comments"

See https://gist.github.com/dvandersluis/86eb48649747c763dd06fb1bc35a4321 for a failing test.

Expected behavior

post.comments.to_sql should return

SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1

Actual behavior

Unscopes association where clause

SELECT "comments".* FROM "comments"

System configuration

Rails version: 6.0.3

Ruby version: 2.6.2

@dvandersluis dvandersluis changed the title unscope(:where) removes association conditions Querying association with unscope(:where) from instance removes conditions May 13, 2020
@dvandersluis
Copy link
Contributor Author

dvandersluis commented May 13, 2020

I'd be happy to try to fix it if someone can point me in the right direction of where the unscope is applied. 😄

@kamipo
Copy link
Member

kamipo commented May 14, 2020

I had also tried to fix it in #38929, but some tests expects to unscope required conditions for now.

@dvandersluis
Copy link
Contributor Author

@kamipo maybe those tests don't make sense then?

@rails-bot
Copy link

rails-bot bot commented Aug 12, 2020

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 6-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 rails-bot bot added the stale label Aug 12, 2020
@dvandersluis
Copy link
Contributor Author

@kamipo i saw #38929 was merged does that mean this was fixed?

@rails-bot rails-bot bot removed the stale label Aug 12, 2020
@rails-bot
Copy link

rails-bot bot commented Nov 10, 2020

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 6-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 rails-bot bot added the stale label Nov 10, 2020
@rails-bot rails-bot bot closed this as completed Nov 17, 2020
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

2 participants