Forum: Ruby on Rails How to do a find with conditions that contain an OR

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.
xiheng xu (Guest)
on 2006-02-11 01:00
(Received via mailing list)
Hi, I'm trying to do a find with a condition that has an OR on the same
column.  Say I only want to use find() but not find_by_sql().  I reduced
my problems into the following:

 values = []
 ... # filling up values from params
 Order.find(:all,
            :conditions => ["(action = ? OR action = ?) AND price = ?",
                             values]

For example I wanted the condition to be
  "(action = 'SELL' OR action = 'SHORT') AND price = '10.00'"

Here I can't use <values> as a hash because the keys for the two
<action> values would be the same.  I tried to use the variable values
as an array, then as a comma-separated string.  Both gave me the
following error:
    wrong number of bind variables (1 for 3) in:
    (action = ? OR action = ?) AND price = ?

I didn't find any answers from the Agile book.  Can anyone help?  Thanks
very much!

- RoR newbie -
Eric G. (Guest)
on 2006-02-11 01:12
(Received via mailing list)
Hi,
You have to have them seperate.
ie.
Order.find(:all,:conditions => ["(action = ? OR action = ?) AND price =
?", action, action, price])

Eric



xiheng xu wrote:
> For example I wanted the condition to be
> very much!
>
> - RoR newbie -
>
>


--
Eric G.
http://www.ericgoodwin.com
Jeremy E. (Guest)
on 2006-02-11 04:57
(Received via mailing list)
On 2/10/06, Eric G. <removed_email_address@domain.invalid> wrote:
> Hi,
> You have to have them seperate.
> ie.
> Order.find(:all,:conditions => ["(action = ? OR action = ?) AND price = ?", action, 
action, price])

Not sure if this works, but you could try:

Order.find(:all,:conditions => ["action IN (?) AND price = ?", actions,
price])
xiheng xu (Guest)
on 2006-02-13 17:31
(Received via mailing list)
Thanks guys!

No Jeremy, that didn't work for me..  I came to understand my question
is really not about using an OR in the find query.

The problem is that I actually construct the query as well as the
parameter values from users input before i put them into the find().
This comes from the fact that I have a big form with almost every text
field being optional for the ultimate selection in the legacy table.

I think I can use the unsafe approach of embedding the values into the
doublequotes.  But that's not recommended, is it?  The Agile book on
p.206 says "This condition can be either a string containing SQL or an
array containing SQL and substitution values".  I took it literally and
that's why I pumped both the query and the arguments into one array on
the RHS of the :conditions =>...

Is there solutions other than having everything within double quotes?

Thanks to all!
-xiheng

On Friday, February 10, 2006, at 6:57 PM, Jeremy E. wrote:
>actions, price])
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>http://lists.rubyonrails.org/mailman/listinfo/rails


- RoR newbie -
xiheng xu (Guest)
on 2006-02-13 17:33
(Received via mailing list)
Thanks guys!

No Jeremy, that didn't work for me..  I came to understand my question
is really not about using an OR in the find query.

The problem is that I actually construct the query as well as the
parameter values from users input before i put them into the find().
This comes from the fact that I have a big form with almost every text
field being optional for the ultimate selection in the legacy table.

I think I can use the unsafe approach of embedding the values into the
doublequotes.  But that's not recommended, is it?  The Agile book on
p.206 says "This condition can be either a string containing SQL or an
array containing SQL and substitution values".  I took it literally and
that's why I pumped both the query and the arguments into one array on
the RHS of the :conditions =>...

Is there a solution other than having everything within double quotes?

Thanks to all!
-xiheng

On Friday, February 10, 2006, at 6:57 PM, Jeremy E. wrote:
>actions, price])
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>http://lists.rubyonrails.org/mailman/listinfo/rails


- RoR newbie -
This topic is locked and can not be replied to.