I am trying to compare the value from a datetime field in my database which is due_date to the current datetime. This needs to happen during the loop in the index so that I can compare the due_date of each record to that of the current datetime? I am at a loss as how to do the comparison. What is the proper way for me to reference just the due_date of the current @item object in the index loop. I know that the code should not go in the view, I'll probably define a helper once I can get the comparison correct.
Asked
Active
Viewed 576 times
0
-
1Assuming `@items` are being set in the controller to all the relevant records, `@items.each |item| { |item| item.due_date > DateTime.now }` will compare each of the item's due_date to the current time. DateTime objects can be compared directly. Does this answer your question? – Prakash Murthy Jan 03 '15 at 00:32
-
Mostly, is the second |item| some type of cast? When ever I used item.due_date, I would get a "due_date is not a method error. item[:due_date] seems to work. My only other question is where should this logic ultimately be? because it is so small, is putting in the view acceptable or should it be defined elsewhere such as a helper. – Michael Brooks Jan 03 '15 at 00:47
-
1The logic can be in the view, or in the helper. Put it in the view first; you can refactor it to a helper later on. In my previous comment, there should have been only one `|item|` within the `{}`. No casting there; it is the ruby bracket syntax for do; see http://stackoverflow.com/a/2122457/429758 Looks like the `item` in your case is not an active record object if `item.due_date` is failing. – Prakash Murthy Jan 03 '15 at 00:55
-
item[:due_date] seemed to work but I am getting strange results. Any test done with a date of today and a later time is coming back past due, but If I set the deadline to tomorrow, Some are past due and some are not. I dont even see any logic to why. My DateTime values are utc and I am cheking them against DateTime.now.utc Any ideas as to why? – Michael Brooks Jan 03 '15 at 01:09
-
I've tried every possible conversion I can think of and the times are not lining up. DateTime.now.utc - DateTime.now.to_i, Time.now etc... – Michael Brooks Jan 03 '15 at 01:43
-
1Use `DateTime.current` instead of `.now` as it is timezone aware; I wrote a blog post about rails and timezones which you may find useful - http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/ – house9 Jan 03 '15 at 18:21
-
Also - does `due_date` need to be a datetime? or is it really just a date, not having to worry about time makes everything much easier, consider renaming it to `due_at` if it is actually a datetime, otherwise change the database type to date. – house9 Jan 03 '15 at 18:23
-
@house9 DateTime.current is still returning a time that is UTC (5hrs ahead of my timezone (eastern). The only thing I have gotten to work is to add 18000 to my due_date.to_i or subtract that from DateTime.now/current.to_i. That will only flag items as overdue correctly if they are in eastern standard. How can I get DateTime to return a time that is based on users current timezone? – Michael Brooks Jan 05 '15 at 17:41
-
I ended up having to do this but it seems pretty hacky <% time_offset = Time.now.utc_offset %> <% if (item[:due_date].to_i - time_offset) < Time.now.to_i %> – Michael Brooks Jan 05 '15 at 18:28
1 Answers
1
if (@book.due_date - Date.today) > 2.days
# DO you like
end
or
if @book.due_date > (Date.today + 2.days)
# DO you like
end

Shiva
- 11,485
- 2
- 67
- 84