This project is archived and is in readonly mode.

#4146 ✓invalid
Valentine Bichkovsky

Multiline string in 'where' condition produces incorrect SQL

Reported by Valentine Bichkovsky | March 10th, 2010 @ 01:58 PM

This piece of code:

class Asset
...
 scope :by_text, lambda {|text|
  unless text.blank?
   includes(:tags).where(
   "(asset_translations.locale = :loc and 
   (asset_translations.name like :expr or 
   asset_translations.description like :expr)) or 
   (tags.name = :name and tags.locale = :loc)",
   {:expr => "%#{text}%", :name => text, :loc => I18n.locale.to_s} )
  end
 }
...
end
produces incorrect SQL in Ruby 1.8.7:
Asset.by_text('foo').to_sql
=> "SELECT     assets.* FROM       assets 
WHERE     ((asset_translations.locale = 'en-US' and \n) 
AND (                              (asset_translations.name like '%foo%' or \n) 
AND (                               asset_translations.description like '%foo%')) or \n) 
AND (                             (tags.name = 'foo' and tags.locale = 'en-US'))"

Works fine in Ruby 1.9.1, or (for Ruby 1.8.7) when line endings are escaped like that:

 scope :by_text, lambda {|text|
  unless text.blank?
   includes(:tags).where(
   "(asset_translations.locale = :loc and \
   (asset_translations.name like :expr or \
   asset_translations.description like :expr)) or \
   (tags.name = :name and tags.locale = :loc)",
   {:expr => "%#{text}%", :name => text, :loc => I18n.locale.to_s} )
  end
 }

BTW, how do I post arbitrarily indented code here?

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages