0

I'm having a strange issue when using the Where clause in Rails. I imagine it has something to do with my OR operator syntax. Here is my query:

 Cookie.where("ID = ? OR ID = ? OR ID = ? OR ID = ?", chocolate.to_s, sugar.to_s, peanut_butter.to_s, oatmeal.to_s) 

When I attempt to execute the query my application just hangs endlessly. However, If I submit a query that looks like this:

 Cookie.where("ID = ?", chocolate.to_s) 

I get the expected result. Any help with the proper format to this where clause would be greatly appreciated. Thanks.

UPDATE I went into the rails console and did as you suggested. This was the result:

 SELECT \"TBLCookies\".* FROM \"TBLCookies\" WHERE (ID in ('4','3','2','1')) 

This seems correct. I opened up SQL Developer, pasted this query in, and ran it. I got the expected result. So everything seems to be fine there. However, when I try to run the query in the rails console I get nothing back. The console just hangs indefinitely. Any insight into what could be going wrong would be great.

1 Answer 1

1

Why not do this

Cookie.where("ID in (?)",[chocolate, sugar, peanut_butter, oatmeal].collect(&:to_s)) 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help Viren, but the syntax that you gave me also makes my app hang indefinitely. I wonder if it matters that I am using an Oracle DB?
@CookieEmpire Just test the command on Oracle do .to_sql on the above command and run it on Oracle Console see if it return any error or so never worked with Oracle as such but I think it should work fine since Mysql and Oracle use the same syntax if feel
I updated my initial post with the output from the rails console. The SQL syntax is correct. There has got to be some other weird issue going on here. . .
@CookieEmpire I ask you run that command in ORACLE CONSOLE does the command run over there I not which version of Rails are you using If latest try add explain to what the problem
Unfortunately, I do not have access to the Oracle Console.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.