user924088
user924088

Reputation: 107

PostgreSQL in Rails query is not working

New to PostgreSQL (and only 6 months more experienced in programming in general), having problems with following query:

TutoringSession.where("extract (DOW from begin_time)
                     = extract DOW from (?)", Time.now)

Gives me the following error message:

ActiveRecord::StatementInvalid: PG::Error: ERROR:  syntax error at or near "DOW"
LINE 1: ..."  WHERE (extract (DOW from begin_time) = extract DOW from (...
                                                             ^
: SELECT "tutoring_sessions".* FROM "tutoring_sessions"  WHERE (extract (DOW from   begin_time) = extract DOW from ('2012-04-18 14:39:58.202249'))

I have looked through the PostgreSQL documentation. Any ideas?

Upvotes: 1

Views: 839

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656331

Updated after comment.
You misplaced a parenthesis. And add an explicit type cast.

TutoringSession.where("extract (DOW from begin_time)
                     = extract (DOW from timestamp ?)", Time.now)

If you just want to insert the current time, you can let PostgreSQL do that for you - provided the time on the database server works for you. Consider time zones.

TutoringSession.where("extract (DOW from begin_time)
                     = extract (DOW from now())")

The manual about extract().

Upvotes: 2

Related Questions