Forum: Ruby on Rails How to escape ? in queries

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1e92f6d8da70aecf8a1f8edb27454b55?d=identicon&s=25 Crazy (Guest)
on 2007-04-19 02:34
(Received via mailing list)
Hi,

  I have a simple table with id and name fields and which has some
data which includes ? and '' characters. If I try to find the rows
which have these elements, the query fails with the following error:

irb(main):013:0* a = Person.find(:all, :conditions => ["name = ???"])
ActiveRecord::PreparedStatementInvalid: wrong number of bind variables
(0 for 3) in: name = ???
        from /activerecord-1.15.2/lib/active_record/base.rb:1463:in
`raise_if_bind_arity_mismatch'
        from /activerecord-1.15.2/lib/active_record/base.rb:1426:in
`replace_bind_variables'
        from /activerecord-1.15.2/lib/active_record/base.rb:1417:in
`sanitize_sql_array'
        from /activerecord-1.15.2/lib/active_record/base.rb:1388:in
`sanitize_sql'
        from /activerecord-1.15.2/lib/active_record/base.rb:1164:in
`add_conditions!'
        from /activerecord-1.15.2/lib/active_record/base.rb:1097:in
`construct_finder_sql'
        from /activerecord-1.15.2/lib/active_record/base.rb:997:in
`find_every'
        from /activerecord-1.15.2/lib/active_record/base.rb:418:in
`find'
        from (irb):13
        from /usr/lib/site_ruby/1.8/rubygems.rb:78


So my question is: is there any way to escape the query ? Or better
still, can I do the query without SQL (pure ruby) ?

Thanks!
3726bda7f0d852f7e8296fb0d69aa9e0?d=identicon&s=25 Andy Triboletti (Guest)
on 2007-04-19 02:58
(Received via mailing list)
>  Or better still, can I do the query without SQL (pure ruby) ?


There sure is.  Try this:

a = Person.find_by_name("???")

Andy
This topic is locked and can not be replied to.