2016-02-07 12 views
0

人们可以随着时间范围这样搜索模式搜寻有时间范围的替代模式

User.where("updated_at = :updated_at and status != :status", 
      updated_at: previous_update..now, status: "employee") 

而且事实证明,

User.where("updated_at = :updated_at", updated_at: previous_update..now) 

不起作用。看起来像应该以某种方式转换为sql格式,然后才能像这样替换。或者,有更好的方式来表达上面的复杂查询?

编辑

我用Jon's suggestion到链where过滤器:

now  = Time.now 
updates = User.where(updated_at: previous_update..now) 

employee_updates  = updates.where(status: "employee") 
non_employee_updates = updates.where('status != :status', status: "employee") 

回答

1

你可以ch艾因您where查询:

User.where(status: 'employee').where(updated_at: previous_update..now) 

或者您可以在同一where子句中结合他们:

User.where(status: 'employee', updated_at: previous_update..now) 

有了您的占位符的条件下,你需要用他们在大括号:

User.where('updated_at = :updated_at and status != :status', 
      {updated_at: previous_update, status: "employee"}) 

虽然无法检查日期范围内的平等,所以检查updated_at等于将会失败。您需要生成大于或等于开始日期并且小于或等于结束日期的各种条件。

基于此......我可能会坚持使用非占位符格式来进行这样的简单查询。

1

您可以使用:

User.where("updated_at >= :updated_at", updated_at: previous_update) 

,如果你需要到现在为止...

+0

感谢您的回复。在我目前的应用程序中,我想确切地知道“现在”。但是这种方法可能适用于另一种情况。 – Adobe