tyliong
2008-Dec-09 20:00 UTC
Active record :find having an if statement inside the conditions
Hi, i was just wondering if its is possible to put a if statment inside the find conditions for example: grovery_price = model.find(:first, :conditions => [ "if x = ? then y must = ?", 200, 300] I have been googling for some time for a solution to this but i just can''t seem to find any. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Dec-09 20:12 UTC
Re: Active record :find having an if statement inside the conditions
Tyl Tan wrote:> Hi, i was just wondering if its is possible to put a if statment > inside the find conditions > > > for example: > > grovery_price = model.find(:first, :conditions => [ "if x = ? then y > must = ?", 200, 300] > > I have been googling for some time for a solution to this but i just > can''t seem to find any.Does :conditions => ["(x = ? and y = ?) or x <> ?", 200, 300, 200] work? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Dec-09 20:14 UTC
Re: Active record :find having an if statement inside the conditions
Or is it just: :conditions => ["(x = ? and y = ?)", 200, 300] i.e., do you want the items where x != 200 or not? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Dec-09 20:29 UTC
Re: Active record :find having an if statement inside the conditions
If x equal 200 then I want the find to check that y is equal to 300 but if x is not equal to 200 there should be no check with y -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Ar Chron Sent: Wednesday, December 10, 2008 4:14 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Active record :find having an if statement inside the conditions Or is it just: :conditions => ["(x = ? and y = ?)", 200, 300] i.e., do you want the items where x != 200 or not? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Dec-09 20:32 UTC
Re: Active record :find having an if statement inside the conditions
Tyl Tan wrote:> If x equal 200 then I want the find to check that y is equal to 300 but > if x is not equal to 200 there should be no check with yWell, :conditions => ["x <> ? or (x = ? and y = ?)", 200, 200, 300] If SQL short-circuits the logic, that shouldn''t check y when x != 200 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Dec-09 20:49 UTC
Re: Active record :find having an if statement inside the conditions
Thanks I''ll try it out when I have the time -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Ar Chron Sent: Wednesday, December 10, 2008 4:33 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Active record :find having an if statement inside the conditions Tyl Tan wrote:> If x equal 200 then I want the find to check that y is equal to 300 but > if x is not equal to 200 there should be no check with yWell, :conditions => ["x <> ? or (x = ? and y = ?)", 200, 200, 300] If SQL short-circuits the logic, that shouldn''t check y when x != 200 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---