Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Subquery generation when using a Relation as a parameter in regular where() #414

Open
gamov opened this issue Feb 23, 2016 · 4 comments
Open

Comments

@gamov
Copy link

gamov commented Feb 23, 2016

I've noticed that in my updated app, subqueries are generated when I supply a relation in a regular where():

Draft.where(id: Draft.limit(2)) 
# => SELECT "drafts".* FROM "drafts" WHERE "drafts"."id" IN (SELECT "drafts"."id" FROM "drafts" LIMIT 2)

This introduces a subquery instead of standard Rails array of ids:

# => SELECT "drafts".* FROM "drafts" WHERE "drafts"."id" IN (1,2)

IMO, Squeel should only work this kind of magic inside a where{} block.

@mckinnsb
Copy link

Could you post your Gemfile.lock? ( or at relevant gem info, Active Record, Squeel, etc )

@gamov
Copy link
Author

gamov commented Feb 29, 2016

Of course! Do you think it's the standard behaviour?

activerecord (4.0.13)
  activemodel (= 4.0.13)
  activerecord-deprecated_finders (~> 1.0.2)
  activesupport (= 4.0.13)
  arel (~> 4.0.0)
arel (4.0.2)
  arel-helpers (2.2.0)
  activerecord (>= 3.1.0, < 5)
ransack (1.6.5)
  actionpack (>= 3.0)
  activerecord (>= 3.0)
  activesupport (>= 3.0)
  i18n
  polyamorous (~> 1.1)
squeel (1.2.3)
  activerecord (>= 3.0)
  activesupport (>= 3.0)
  polyamorous (~> 1.1.0)

PS: I removed the information about the update since I could also see it under Rails 3.2 -> (Rails 4.0), forgot the 'updated' word...

@kimrgrey
Copy link

kimrgrey commented Mar 1, 2016

👍 for this issue, it breaks existing complex subquery in Rails 4 application

@mckinnsb
Copy link

mckinnsb commented Mar 1, 2016

I'm pretty sure this is standard behavior, see: #272 for examples of expected behavior re: subqueries and Rails.

If you need "id" IN (1,2), I think I would just .to_a.map &:id ( or perhaps just to_a would give you what you want, maybe the magic is in recognizing that you passed an CollectionProxy ).

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

No branches or pull requests

3 participants