Shakered
Shakered

Reputation: 71

How to write a query to compare current date to created_at timestamps in database?

I would like to write a query to compare created_at timestamp with my current date in Ruby on Rails using ActiveRecord and I don't quite know how do it yet

Date is stored like this -> created_at: 2019-06-03 03:30:40

Upvotes: 0

Views: 107

Answers (2)

user11581234
user11581234

Reputation:

It's straightforward in Ruby

@user.created_at > Time.now

Upvotes: 0

Max Vinicius
Max Vinicius

Reputation: 647

Try this:

<% if @user.created_at > 5.days.ago.beginning_of_day %>
  <span>New User!</span>
<% end%>

Or you can use:

<%= time_ago_in_words(@user.created_at) %>

Source: https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words

Upvotes: 1

Related Questions