ruby string format kicks in when doign wild card in activerecord

Discussion in 'Ruby' started by Junkone, Jun 15, 2008.

  1. Junkone

    Junkone Guest

    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.

    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
     
    Junkone, Jun 15, 2008
    #1
    1. Advertisements

  2. Junkone

    Sam Smoot Guest

    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#%.
     
    Sam Smoot, Jun 15, 2008
    #2
    1. Advertisements

  3. Junkone

    Junkone Guest

    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
     
    Junkone, Jun 16, 2008
    #3
  4. Hi --

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


    David
     
    David A. Black, Jun 16, 2008
    #4
  5. Junkone

    Eric Hodel Guest

    Or, escape it with another %.

    irb(main):003:0> "processed_indicator like 'LF%%'" % []
    => "processed_indicator like 'LF%'"
     
    Eric Hodel, Jun 16, 2008
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.