Quote ActiveRecord::Relation with their SQL queries #10474

Closed
wants to merge 1 commit into
from

4 participants

@crodjer

This makes writing nested queries simpler, without any need for explicit
to_sql call. In some situations, where using JOIN becomes too
complicated, nested queries come in handy.

@neerajdotname
Ruby on Rails member

@crodjer can you elaborate on your statement that this makes writing nested queries simpler ? Can you show a version without the patch and the version with the patch ?

@rafaelfranca
Ruby on Rails member

I don't think this will work. When you call to_sql Active Record can generate the SQL statement with binds placeholders, and your SQL query will fail.

@crodjer
@rafaelfranca
Ruby on Rails member

I'm pretty sure that Profile.select("user_id").where(gender: 'm').to_sql in PostgreSQL will generate SELECT "profiles"."user_id" FROM "profiles" WHERE "profiles"."gender" = ? and this will not work.

@crodjer

But the tests seem to be passing for me, while I ran the test suite with postgresql backend. The subquery that I had is, Default.select('id').where('id > ?', 0)

@crodjer crodjer Quote ActiveRecord::Relation with their SQL queries
This makes writing nested queries simpler, without any need for explicit
`to_sql` call. In some situations, where using `JOIN` becomes too
complicated, nested queries come in handy.

Signed-off-by: Rohan Jain <crodjer@gmail.com>
a511d48
@gregmolnar

This issue is stale.

@crodjer crodjer closed this May 2, 2014
@rafaelfranca
Ruby on Rails member
@gregmolnar

@rafaelfranca Thanks for the heads up! I will do so next time.

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