0

很奇怪:我的示波器运行良好:

scope :starting_at, lambda {|start_date|
    where("CAST(starts_at AS TIME) = ?", Event.db_time(start_date))
}

(rdb:1) sd1 => 2012-12-16 10:00:00 +0100
Event.starting_at(sd1)
[#<Event id: 1, account_id: 1, place_id: 1, slug: "my-new-....

我有另一个类似的范围,它引发了一个错误:

scope :starting_at_between, lambda {|start_time, end_time|
    where( "? <= CAST(starts_at AS TIME) <= ?", Event.db_time(start_time), Event.db_time(end_time) )
}
(rdb:1) sd2  =>  2012-12-15 08:00:00 +0100
(rdb:1) ed2  =>  2012-12-19 23:00:00 +0100
Event.starting_at_between(ed2, sd2)
INTERNAL ERROR!!! Mysql2::Error: Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERIC) for operation '<=': SELECT `events`.* FROM `events`  WHERE ('23:00:00' <= CAST(starts_at AS TIME) <= '08:00:00')

使用这个有什么问题...?不是因为 2 个参数.. 我有一个类似的范围,其中 CAST 是 AS DATE 并且运行良好...

scope :starting_on_between, lambda { |start_date, end_date|
    where( "? <= CAST(starts_at AS DATE) <= ?", Event.db_date(start_date), Event.db_date(end_date) )
}
Event.starting_on_between(ed2, sd2)
[#<Event id: 1, account_id: 1, place_id: 1, slug:...
4

1 回答 1

0

错误的 CAST .. 应该在过滤器值上完成......而不是在 db 记录属性上......

范围:starting_at_between, lambda {|start_time, end_time| 其中(“CAST(?AS TIME)<= starts_at <= CAST(?AS TIME)”,Event.db_time(start_time),Event.db_time(end_time))}

于 2012-12-05T18:23:16.453 回答