joeri
joeri

Reputation: 141

With Rails 4.2.3 changing the behavior of where_values, how do I get valid SQL for where_values?

An ActiveRelation instance has where_values. In Rails 4.2.2 and earlier the following code

class Post < ActiveRecord::Base
  scope :staff_picks, -> { where(staff_pick: true) }
end

puts Post.staff_picks.where_values[0].to_sql 

for Postgresql returned

"posts"."staff_pick" = 't'

and that was correct.

Rails 4.2.3 breaks this behavior, the same code returns

"posts"."staff_pick" = $1

How do I get the same valid SQL in Rails 4.2.3 and 4.2.4?

Upvotes: 1

Views: 1196

Answers (1)

phillbaker
phillbaker

Reputation: 1548

This did change in Rails 4.2.3, but the original behavior, without the binding, can be obtained by updating the scope to use AREL:

class Post < ActiveRecord::Base
  scope :staff_picks, -> { where(arel_table[:staff_pick].eq(true)) }
end

puts Post.staff_picks.where_values[0].to_sql # => "`posts`.`staff_pick` = 1" 

This is also backwards compatible to Rails 4.1 and Rails 3.2.

Upvotes: 3

Related Questions