0

I am trying to execute query below but I get an error.

Event.where("id IN ? and event_start_date > ?",[1, 2],Time.now)

I get error as follows:

SELECT "events".* FROM "events" WHERE (id IN 1,2 and event_start_date > '2015-03-11 04:40:43.819487')
PG::SyntaxError: ERROR:  syntax error at or near "1"
LINE 1: SELECT "events".* FROM "events" WHERE (id IN 1,2 and event_s...

Parameter value is not consider as an array.

4 Answers 4

5

You have a syntax error. I think you can make your code easier to read, which would help prevent this kind of error:

Event.where({ id: [1, 2], event_start_date: Time.now })

In this format you pass a hash to the where query with id and event_start_date as the hash keys, and [1, 2] (an array) and Time.now as the values.

This is much more readable and easier to debug.

Sign up to request clarification or add additional context in comments.

1 Comment

With this solution, the query will only looks for Events with a start date of EXACTLY "now." Consider revising your answer to include the greater-than condition used in the question.
3

For a combination of an array argument and a range operation, I would do this as I find it more readable:

Event.where(id: [1, 2]).where("event_start_date > ?", Time.now)

Comments

1

@sank : your error gives your answer

there is syntax error in your query...

Instead of this

Event.where("id IN ? and event_start_date > ?",[1, 2],Time.now)

try this :

Event.where("id IN (?) and event_start_date > (?)",[1, 2],Time.now)

put () beside ?.

Comments

0
Event.where("id IN(?) AND event_start_date < (?)", [1,2,3], Time.now)

You're missing the () around the ? mark sign.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.