我正在尝试结合两个常见的活动记录查询。
- 我需要在日期字段上执行where子句
- 我需要加入2张桌子
由于我要加入他们,因此我需要指定列的表,两个表都具有一个列,created_at
。
我基本上需要结合以下两个答案:
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