PostgreSQL新手(编程经验一般只有6个月),对以下查询有问题:

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

给出以下错误消息:
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'))

我已经看过PostgreSQL文档。有什么想法吗?

最佳答案

评论后更新。
你放错了括号。并添加显式类型转换。

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()

10-08 04:20