mysql - Rails where 子句与 Between 在 mysql 中不起作用

标签 mysql ruby-on-rails

我在 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/

相关文章:

ruby-on-rails - 需要知道如何在 ruby​​ 中处理散列键

mysql - SQL 更新,子查询返回多行

PHP MySQL 查询不起作用,但可以从终端运行

php - 如何在php中显示来自特定mysql数据库的所有表名

ruby-on-rails - Ruby 救援和最佳实践语法

ruby-on-rails - Ruby on Rails devkit 窗口

mysql - ActiveRecord 相当于这个

php - 使用 PDO 更新多列和多行

mysql - Excel CSV 到 Mysql 数据库

mysql - 选择名称和行数?