xiheng xu
2006-Feb-10 23:00 UTC
[Rails] How to do a find with conditions that contain an OR
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 - -- Posted with http://DevLists.com. Sign up and save your time!
Eric Goodwin
2006-Feb-10 23:12 UTC
[Rails] How to do a find with conditions that contain an OR
Hi, You have to have them seperate. ie. Order.find(:all,:conditions => ["(action = ? OR action = ?) AND price = ?", action, action, price]) Eric xiheng xu wrote:> 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 Goodwin http://www.ericgoodwin.com
Jeremy Evans
2006-Feb-11 02:57 UTC
[Rails] How to do a find with conditions that contain an OR
On 2/10/06, Eric Goodwin <ruby@ericgoodwin.com> 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
2006-Feb-13 15:30 UTC
[Rails] How to do a find with conditions that contain an OR
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 Evans wrote:>On 2/10/06, Eric Goodwin <ruby@ericgoodwin.com> 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]) >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails- RoR newbie - -- Posted with http://DevLists.com. Sign up and save your time!
xiheng xu
2006-Feb-13 15:32 UTC
[Rails] How to do a find with conditions that contain an OR
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 Evans wrote:>On 2/10/06, Eric Goodwin <ruby@ericgoodwin.com> 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]) >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails- RoR newbie - -- Posted with http://DevLists.com. Sign up and save your time!