Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3, comparing only the dates of two datetime columns in rails

Lets say I want to compare the dates only of two datetime columns within 1 record. So I don't want the time looked at.

I.e.

viewed_date, and updated_at (I added viewed_date) are two datetime formats, but I only want to see if they occurred on the same day or days apart. The problem with datetime is that its comparing the times, which is just too specific for me right now.

Thanks

-Elliot

like image 668
Elliot Avatar asked Jan 22 '11 19:01

Elliot


1 Answers

Declare a new attribute that contains just the date:

class WhateverModel < ActiveRecord::Base
  ...
  def viewed_date_only
    viewed_date.to_date
  end
end

Use that for your comparison in the controller or wherever.

like image 91
Jakob Borg Avatar answered Sep 25 '22 16:09

Jakob Borg