我想在ActiveRecord / Arel中编写一个查询(没有硬编码SQL),测试日期是在两列之间:例如WHERE input_date BETWEEN some_table.column_1 AND some_table.column_2
。我尝试了以下内容:
between_query = Arel::Nodes::Between.new(
Time.zone.now,
Arel::Nodes::And.new(
[
arel_table[:column_1],
arel_table[:column_2]
]
)
)
where(between_query)
但我收到错误Arel::Visitors::UnsupportedVisitError: Unsupported argument type: Time. Construct an Arel node instead.
答案 0 :(得分:2)
您可以尝试构建一个Arel节点:
between_query = Arel::Nodes::Between.new(
Arel::Nodes::Quoted.new(Time.zone.now),
Arel::Nodes::And.new(
[
arel_table[:column_1],
arel_table[:column_2]
]
)
)
where(between_query)
答案 1 :(得分:0)
您需要用某种SQL节点替换Time.zone.now
。如果您只是想查看当前时间是否在两列之间,那么这应该有效:
between_query = Arel::Nodes::Between.new(
Arel.sql('current_date'),
Arel::Nodes::And.new(
[
arel_table[:column_1],
arel_table[:column_2]
]
)
)
where(between_query)
否则,如果您尝试传入可变时间而不是Arel.sql('current_date')
,请将其替换为Arel::Nodes::Quoted.new(Time.zone.now.to_s(:db))