【发布时间】:2019-11-14 02:49:14
【问题描述】:
我正在尝试组合两个常见的活动记录查询。
- 我需要对日期字段执行 where 子句
- 我需要加入 2 个表
由于我要加入他们,我需要指定列的表,两个表都有一个列,created_at。
我基本上需要结合这2个答案:
ActiveRecord joins throwing Column in where clause is ambiguous error - Rails 5.1
@projects = @projects.joins(:categories).where(categories: { id: params[:category_id] }) if params[:category_id].present?
Active Record - Find records which were created_at before today
MyModel.where("created_at < ?", 2.days.ago)
我现在拥有的是这样的:
Train.joins(:schedule).where('created_at >= ?', DateTime.now - 7.days)
返回:
Mysql2::Error: Column 'created_at' in where clause is ambiguous: SELECT
【问题讨论】:
-
您要在哪个表上使用 WHERE 子句?还是两者兼而有之?
-
我正在尝试在火车上使用它
标签: ruby-on-rails ruby activerecord