3

In rails 2 you could use proxy_options to see the options for specific database queries. In rails 3 it has been removed. Is there a different option for getting this same info?

Lan
  • 6,039
  • 7
  • 22
  • 24
  • Possible duplicate - http://stackoverflow.com/questions/3025103/how-to-test-a-scope-in-rails-3 – sethvargo Jan 11 '11 at 04:49
  • Not a dupe, the linked question is about how to test a scope. My question is about knowing what the scoped query looks like. – Lan Jan 11 '11 at 05:40

2 Answers2

2

I posted a long answer here: How to test a scope in Rails 3

But the main answer is use #where_values_hash or #to_sql on your scope.

Community
  • 1
  • 1
adzdavies
  • 1,545
  • 1
  • 13
  • 13
  • Or `where_values`. So you might do: `User.joins(:address).where(Address.foreign.where_values)`. – Henrik N Feb 21 '12 at 15:39
  • Note: `where_values_hash` doesn't seem to work everywhere; doesn't work when chained with `not` (including scopes chained as such). Reference: https://github.com/rails/rails/issues/23613 (doesn't look like it's being worked on at this time) – Jay Dorsey Mar 09 '20 at 22:28
1

I only now learned of a nicer way. Undocumented but nice:

User.joins(:address).merge(Address.foreign)

as an alternative to

User.joins(:address).where(Address.foreign.where_values)
Henrik N
  • 15,786
  • 5
  • 82
  • 131