出于某种原因,当我在 where 中使用时,Date 类被识别
scope :with_active_subthing, joins("left outer join thing_subthing on thing_subthing.patient_id = thing.id").where("thing_subthing.start_date <= ? AND ( thing_subthing.end_date > ? OR thing_subthing.end_date IS NULL ) ", Date.today, Date.today)
但是当我在连接中使用它时无法识别它
scope :with_active_subthing, joins("left outer join thing_subthing on thing_subthing.patient_id = thing.id AND (thing_subthing.start_date <= ? AND ( thing_subthing.end_date > ? OR thing_subthing.end_date IS NULL )) ", Date.today, Date.today)
我得到:
RuntimeError: unknown class: Date
最佳答案
如果您查看 underlying code that builds the join portion ,你可以看到它正在寻找特定的对象类型,而 Date 不是其中之一。该错误有点令人困惑,但它的意思是 Date 不能用作 joins
的参数。从您的问题中并不清楚为什么要这样做,但是您绝对应该像在第一个示例中那样同时使用连接和位置。
这是导致问题的代码,因为它落入了“raise”行:
def build_joins(manager, joins)
buckets = joins.group_by do |join|
case join
when String
'string_join'
when Hash, Symbol, Array
'association_join'
when ActiveRecord::Associations::JoinDependency::JoinAssociation
'stashed_join'
when Arel::Nodes::Join
'join_node'
else
raise 'unknown class: %s' % join.class.name
end
end
关于sql - 使用 Date.today 加入命名范围的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13781512/