Forum: Ruby on Rails Rails find(:all, :conditions) generates bad SQL?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
srj (Guest)
on 2009-01-03 15:07
(Received via mailing list)

What is wrong with my finder method?  I have the following (using
Rails 2.2.2):

         active_rows = Model.find(:all, :conditions =>  ["status

ACTIVE_STATUSES is a constant array with 4 status codes in it.  The
above rails line of code results in the following SQL SELECT

  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
Frederick C. (Guest)
on 2009-01-03 15:44
(Received via mailing list)
On Jan 3, 1:06 pm, srj <removed_email_address@domain.invalid> wrote:
> statement:
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}

Phlip (Guest)
on 2009-01-03 19:30
(Received via mailing list)
> 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
srj (Guest)
on 2009-01-05 03:51
(Received via mailing list)
Thank you - find_all_by_status is the right way to go here (I should
have thought of that!)
This topic is locked and can not be replied to.