我在 Rails 中有以下查询...
@registrations = Registration.where(Orientation.where(class_date: @start_date..@end_date))
查询本身不会引发错误,但是如果我尝试检查 @registrations 对变量执行任何操作,则会收到错误...
Cannot visit ActiveRecord::Relation
有什么想法吗?
最佳答案
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')
关于mysql - Rails where 子句与 Between 在 mysql 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21891681/