Rails where子句之间没有在mysql中工作

时间:2014-02-19 20:28:12

标签: mysql ruby-on-rails

我在rails中有以下查询...

@registrations = Registration.where(Orientation.where(class_date: @start_date..@end_date))

查询本身不会抛出错误,但是如果我尝试检查@registrations对变量做任何事情我会得到错误......

Cannot visit ActiveRecord::Relation

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

Orientation.where(class_date: @start_date..@end_date)

您喜欢的范围查询

SELECT * FROM orientations WHERE class_date BETWEEN <start_date> AND <end_date>

<强>更新

Registration.where(created_at: @start_date..@end_date).
  joins(:orientation).
  where('orientations.created_at' => @start_date..@end_date)

将生成以下SQL我认为您需要的内容

SELECT registrations.* FROM registrations INNER JOIN orientations ON orientations.id = registrations.orientation_id WHERE (registrations.created_at BETWEEN '2014-02-17 16:01:41' AND '2014-02-19 16:01:41') AND (orientations.created_at BETWEEN '2014-02-17 16:01:41' AND '2014-02-19 16:01:41')