Forum: Ruby on Rails find_by_sql with named parameterized sql

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.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-01-20 15:39
I tried to use named parameters in my SQL query , using find_by_sql

Order.find_by_sql ([select * from orders where amount > ? and quantity >
?", [ @amount,  @quantity ] works;..
but
Order.find_by_sql ([select * from orders where amount > :amount and
quantity > :quantity ", [ :amount => @amt,  :quantity => @qty ]  is not
working
a I wrong or should I use a plain select string ?

tfyl

kad
70ca58d0e0e0eabbdb74d177417d09d7?d=identicon&s=25 augustlilleaas@gmail.com (Guest)
on 2007-01-20 20:25
(Received via mailing list)
The latter isn't working because you are inserting symbols directly in
the sql, and as far as I know this isn't allowed. What's wrong with
using the first alternative?

On Jan 20, 3:39 pm, Kad Kerforn <rails-mailing-l...@andreas-s.net>
70ca58d0e0e0eabbdb74d177417d09d7?d=identicon&s=25 augustlilleaas@gmail.com (Guest)
on 2007-01-20 20:25
(Received via mailing list)
By the way, I reccomend doing this instead:

@order = Order.find(:all, :conditions => ["amount > ? and quantity >
?", @amount, @quantity]

Just in case you feel like making the orders table a legacy table
called "15_t_orders" or whatever, you're keeping your app DRY by using
the rails finders instead of sql, and only having the data finding
itself in the sql part.

On Jan 20, 8:01 pm, "augustlille...@gmail.com"
This topic is locked and can not be replied to.