Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Multiline string in 'where' condition produces incorrect SQL

#4146

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?

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Valentine Bichkovsky
  2. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid

    If you escape line endings, \n won't be added to the string. At the time 1.9.1 might have escaped line endings automatically but now all 3 rubies (1.9.1, 1.9.2 and 1.8.7) behave in the same manner.

    I'm closing this ticket, but if this is still an issue please make a comment to reopen and I'll oblige.

    June 23rd, 2010 @ 03:12 AM

  3. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:53 PM