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


#1

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


#2

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}

Fred


#3

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.


#4

Thank you - find_all_by_status is the right way to go here (I should
have thought of that!)