Hello,
What is wrong with my finder method? I have the following (using
Rails 2.2.2):
active_rows = Model.find(:all, :conditions => ["status
in ?", ACTIVE_STATUSES])
ACTIVE_STATUSES is a constant array with 4 status codes in it. The
above rails line of code results in the following SQL SELECT
statement:
SELECT * FROM `model` WHERE (status in
''AR'',''GA'',''GP'',''GS'')
This is incorrect SQL - it should be written as:
SELECT * FROM `model` WHERE status in
(''AR'',''GA'',''GP'',''GS'')
Notice that the right-paren is in the wrong place. How can I change
my statement to generate the correct SQL? I always assume that my
code is wrong, but could this be a bug is Rails code?
Thank you
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Jan-03 13:43 UTC
Re: Rails find(:all, :conditions) generates bad SQL?
On Jan 3, 1:06 pm, srj <stephenr.jac...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > What is wrong with my finder method? I have the following (using > Rails 2.2.2): > > active_rows = Model.find(:all, :conditions => ["status > in ?", ACTIVE_STATUSES]) > > ACTIVE_STATUSES is a constant array with 4 status codes in it. The > above rails line of code results in the following SQL SELECT > statement: > > SELECT * FROM `model` WHERE (status in ''AR'',''GA'',''GP'',''GS'') > > This is incorrect SQL - it should be written as: > > SELECT * FROM `model` WHERE status in (''AR'',''GA'',''GP'',''GS'') > > Notice that the right-paren is in the wrong place. How can I change > my statement to generate the correct SQL? I always assume that my > code is wrong, but could this be a bug is Rails code? >your code should read "status in (?)" - rails doesn''t add parens for you at all (apart from surrounding the entire chunk of the conditions in parens). You could also write :conditions => {:status => ACTIVE_STATUSES} Fred> Thank you--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> You could also write :conditions => {:status => ACTIVE_STATUSES}You could also write Model.find_all_by_status(ACTIVE_STATUSES) So the less you do for Rails, the more opportunities it has to do it right. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you - find_all_by_status is the right way to go here (I should have thought of that!) On Jan 3, 12:29 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You could also write :conditions => {:status => ACTIVE_STATUSES} > > You could also write Model.find_all_by_status(ACTIVE_STATUSES) > > So the less you do for Rails, the more opportunities it has to do it right.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---