3

出于某种原因,当我在 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
4

1 回答 1

4

如果您查看构建连接部分的底层代码,您会发现它正在寻找特定的对象类型,而 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
于 2012-12-08T20:32:22.490 回答