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

一笑奈何 提交于 2019-12-11 17:00:52

问题


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


回答1:


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




回答2:


It's straightforward in Ruby

@user.created_at > Time.now



来源:https://stackoverflow.com/questions/56452028/how-to-write-a-query-to-compare-current-date-to-created-at-timestamps-in-databas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!