Ruby string format kicks in when doign wild card in activerecord

when i was doing a LIKE statement in my wild card query. Ruby thinks
that the % is a String Format charactor and throws exception. what are
my options.

Rawdata.find(:all , :conditions=>[“processed_indicator like ‘LF%’”])
ArgumentError: malformed format string - %’
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:1421:in %' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1421:insanitize_sql_array’
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:1390:in sanitize_sql' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1163:inadd_conditions!’
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:1096:in construct_finder_sql' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:997:infind_every’
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:418:in `find’
from (irb):20

On Jun 15, 4:29 pm, Junkone [email protected] wrote:

active_record/base.rb:1421:in `sanitize_sql_array’
from (irb):20

Try it without the Array wrapper? Or with a ? place-holder and ‘LF%’
as the second element in the Array? Or monkey-patch the
sanitize_sql_array method not to use String#%.

On Jun 15, 6:24 pm, Sam S. [email protected] wrote:

Rawdata.find(:all , :conditions=>[“processed_indicator like ‘LF%’”])
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/

  • Show quoted text -

when i use the place holder ? the acticerecord creates a bad sql.
Rawdata.find(:all, :conditions=>[“processed_indicator is NULL and
symbol not in (select distinct r.symbol from rawdatas r
WHERE r.executionecn like ? or r.executionecn like ?”,‘%IDEAL
%’,‘%FX%’])
is the actual definition. it produces an sql like this where it opens
a bracket for the inner select but fails to close it correctly.

a=Rawdata.new
=> #<Rawdata:0x45ee094 @attributes={“updated_at”=>nil, “size”=>nil,
“execution_id”=>nil, “executionecn”=>“”, “account”=>“”, "
tradedatetime"=>nil, “price”=>nil, “symbol”=>“”,
“processing_date”=>nil, “action”=>“”, “processed_indicator”=>nil,
“created_a
t”=>nil}, @new_record=true>
a.unprocessedRawData()
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in
your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ‘’ at line 2:
SELECT * FROM rawdatas WHERE (processed_indicator is NUL
L and symbol not in (select distinct r.symbol from rawdatas r
WHERE r.executionecn like ‘%IDEAL%’ or r.executionecn like ‘%FX
%’)
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/connection_adapters/abstract_adapter.rb:128
:in log' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/connection_adapters/mysql_adapter.rb:243:in execute’
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/connection_adapters/mysql_adapter.rb:399:in
select' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/connection_adapters/abstract/database_state ments.rb:7:in select_all’
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:427:in find_by_sql' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:997:in find_every’
from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
active_record/base.rb:418:in find' from E:/TradingTools/Development/app/models/rawdata.rb:14:in unprocessedRawData’
from (irb):42
SELECT * FROM rawdatas WHERE (processed_indicator is NULL and symbol
not in (select distinct r.symbol from rawdatas r
WHERE r.executionecn like ‘%IDEAL%’ or r.executionecn like ‘%FX
%’)

It created the opening bracket after the FIRST WHERE STATEMETN but it
does not produce a matching closing bracket.
some bug in activerecord. dunno how to fix it up.
seede

Hi –

On Mon, 16 Jun 2008, Junkone wrote:

when i use the place holder ? the acticerecord creates a bad sql.
Rawdata.find(:all, :conditions=>[“processed_indicator is NULL and
symbol not in (select distinct r.symbol from rawdatas r
WHERE r.executionecn like ? or r.executionecn like ?”,’%IDEAL
%’,’%FX%’])
is the actual definition. it produces an sql like this where it opens
a bracket for the inner select but fails to close it correctly.

You’ve opened it, so try closing it yourself.

David

On Jun 15, 2008, at 15:24 PM, Sam S. wrote:

Try it without the Array wrapper? Or with a ? place-holder and ‘LF%’
as the second element in the Array? Or monkey-patch the
sanitize_sql_array method not to use String#%.

Or, escape it with another %.

irb(main):003:0> “processed_indicator like ‘LF%%’” % []
=> “processed_indicator like ‘LF%’”