1

I am trying to compare dates for an app in rails. I have tried this link Comparing dates in rails. But this is not what I want, suppose I have a filter according to dates, then I am doing

<% jobs = @user.jobs.where(:curr_date => (Date.today))%>

Here, curr_date is a "date" attribute chosen by me.

This code works for entries which have today's date, but if I want all entries which have date between today and a week before, what should I do?

Thanx! :)

4

4 回答 4

2

你可以简单地使用

jobs = @user.jobs.where(:curr_date => (DateTime.now..1.week.ago))

更新到DateTime.now或者也可以使用Date.today.to_time,否则比较 aDate和 a会出错Time

于 2012-07-10T11:33:52.013 回答
1

我认为这样的事情应该有效:

<% jobs = @user.jobs.where(['curr_date < ? AND curr_date > ?', Date.today, 1.week.ago])) %>

于 2012-07-10T11:31:30.697 回答
1

compare_date_1 <=> comparison_date_2 可能对您有用,它会相应地返回-1,1,0,您可以填充它们......

于 2012-07-10T11:43:25.663 回答
0

没有在控制台中尝试过,但我认为它应该可以工作:

# You need to implement the #week_before method
@user.jobs.where(:curr_date => (Date.week_before..Date.today))

Active Record Querying - Range 条件下的数组条件检查 (2.2.2)

于 2012-07-10T11:34:35.347 回答