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

Unexpected behaviour for none scope #18897

Closed
bendilley opened this issue Feb 11, 2015 · 9 comments
Closed

Unexpected behaviour for none scope #18897

bendilley opened this issue Feb 11, 2015 · 9 comments

Comments

@bendilley
Copy link

where post_id: Post.none

creates a clause like

"post_id" IN (SELECT "posts"."id" FROM "posts")

which is exactly the same as you get with

where post_id: Post.all

This is contrary to what I think most would expect.

I'm not sure how this particular scenario should be addressed. One option might be to generate something like this instead:

"post_id" IN (SELECT "posts"."id" FROM "posts" WHERE 1 = 0)
@kaspth
Copy link
Contributor

kaspth commented Feb 11, 2015

I don't think the none relation was meant to be used in queries like this.

Honestly I'm having trouble understanding what you're trying to fetch. Won't where(post_id: nil) work?

@senny
Copy link
Member

senny commented Feb 11, 2015

Probably related to #15763

@kaspth the use case I can see is that you have a method which determines the subquery using a relation. That method could return Post.none in some cases and a different relation in others.

@kaspth
Copy link
Contributor

kaspth commented Feb 11, 2015

@senny 👍 that would make sense.

@senny
Copy link
Member

senny commented Feb 11, 2015

@bendilley what version of Rails are you using?

I remember working on that exact issue. See #15176 and c2357eb

@bendilley
Copy link
Author

@senny I'm using Rails 4.0.12

You're right - your commit does address this very problem. From which version of Rails is your fix applied?

@bendilley
Copy link
Author

Oh I see - it's in 4.2.0. Thanks for your help @senny!

Closing as a dupe of #15176

@senny
Copy link
Member

senny commented Feb 11, 2015

@bendilley I just checked and the patch is only applied on 4-2-stable and master. I'll look into a possible backport to 4-1-stable.

As far as I know the last bugfix release for 4.0. is over so you'd have to upgrade.

@bendilley
Copy link
Author

No worries, thanks @senny. I have a serviceable workaround for now - I just wanted to raise it coz it caught me out. Evidently I wasn't alone... I guess I didn't search existing issues thoroughly enough 😅

@senny
Copy link
Member

senny commented Feb 11, 2015

backported the patch to 4-1-stable.

@bendilley thank you for reporting! 💛

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

3 participants