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?