Rails中的活动记录连接查询问题

时间:2016-02-26 05:40:49

标签: mysql ruby-on-rails left-join

我在Rails application使用加入查询,我获得了成功记录,但我需要fetch record更新日期大于昨天日期或创建日期大于昨天。

如何查询OR between updated_date and created_date?

所以我解雇了下面的查询但发布了。

@courses = Course.includes(:translations).where(course_translations: {status: 'active'}, published: true, created_at: time_range, updated_at: time_range)

仍然没有得到任何解决方案。任何人都有想法,请与我分享。

2 个答案:

答案 0 :(得分:0)

嘿,你可以试试这种方式

start_date =  Time.zone.now.beginning_of_day
end_date = Time.zone.now.end_of_day
@courses = Course.includes(:translations).where(course_translations: {status: 'active'}, published: true).where("courses.created_at Between ? And ? or courses .updated_at Between ? And ?", start_date,end_date,start_date,end_date).

答案 1 :(得分:-1)

希望它能提供您想要的输出

Course.includes(:translations).where("course_translations.status =? and courses.published =? and (courses.created_at > ? or courses.updated_at > ?)",'active',true,Date.yesterday.end_of_day,Date.yesterday.end_of_day)

让我知道你的结果