# Find\_by\_sql with named parameterized sql

**URL:** <https://www.ruby-forum.com/t/find-by-sql-with-named-parameterized-sql/82761>\
**Category:** Rails\
**Created:** [January 20, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/find-by-sql-with-named-parameterized-sql/82761 "2007-01-20T15:39:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 20, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/find-by-sql-with-named-parameterized-sql/82761/1 "2007-01-20T15:39:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 20, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/find-by-sql-with-named-parameterized-sql/82761/2 "2007-01-20T20:25:16Z")

</div>

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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 20, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/find-by-sql-with-named-parameterized-sql/82761/3 "2007-01-20T20:25:16Z")

</div>

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”
