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

Suppose I want to compare the dates of only two datetime columns within 1 record. Therefore, I do not want time to look.

those.

seen_date and updated_at (I added visible_date) are two date and time formats, but I only want to see if they occurred on the same day or days from each other. The problem with datetime is that it compares the time, which is now too specific for me.

thank

-Elliot

+5
source share
2 answers

Declare a new attribute containing only the date:

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

Use this for comparison in a controller or anywhere.

+7
source

, : start_date: - "2015-04-06 15:31:43 +0530" end_date: - "2015-04-16 15:31:43 +0530" post_review.all.where( "(created_at:: date > =: start_date) (created_at:: date < =: end_date)", start_date: start_date.strftime( "% Y-% m-% d" ), end_date: end_date.strftime( "% Y-% m-% d" ))

, 6 16

0

All Articles