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.
Kad K. (Guest)
on 2007-01-20 16: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
(Guest)
on 2007-01-20 21: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 K. <removed_email_address@domain.invalid>
(Guest)
on 2007-01-20 21: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, "removed_email_address@domain.invalid"
This topic is locked and can not be replied to.