Find_by_sql with named parameterized sql


#1

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


#2

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


#3

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”