ruby string format kicks in when doign wild card in activerecord

J

Junkone

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:in `sanitize_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:in `add_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: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 (irb):20
 
S

Sam Smoot

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:in `sanitize_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:in `add_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: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 (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#%.
 
J

Junkone

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:in `sanitize_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:in `add_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: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 (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#%.- Hide quoted text -

- 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.
=> #<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>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
 
D

David A. Black

Hi --

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
 
E

Eric Hodel

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 - %'

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%'"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top