【问题标题】:Rails where clause with between not working in mysqlRails where子句在mysql中不起作用
【发布时间】:2014-03-20 10:39:41
【问题描述】:

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

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

查询本身不会引发错误,但是如果我尝试检查 @registrations 对变量执行任何操作,我会收到错误...

Cannot visit ActiveRecord::Relation

有什么想法吗?

【问题讨论】:

标签: mysql ruby-on-rails


【解决方案1】:

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')

【讨论】:

  • 这适用于方向,但不适用于注册。查询 Orientation.where(class_date: @start_date..@end_date) 有效,但查询 Registration.where(Orientation.where(class_date: @start_date..@end_date))。我收到错误无法访问 ActiveRecord::Relation
  • 正确,方向有_many注册,注册属于_to方向。
  • 我更新了我的帖子以显示我在代码中遇到的当前错误。
  • 戴姆儿子……你成功了!非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
相关资源
最近更新 更多