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

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.
D9e51e2cc1f5ab74d3c9cd56a53b6a10?d=identicon&s=25 srj (Guest)
on 2009-01-03 14:07
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-03 14:44
(Received via mailing list)
On Jan 3, 1:06 pm, srj <stephenr.jac...@gmail.com> 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}

Fred
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-01-03 18: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
right.
D9e51e2cc1f5ab74d3c9cd56a53b6a10?d=identicon&s=25 srj (Guest)
on 2009-01-05 02: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.