Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, how to determine if a user was created today?

I would like a way to determine if a User record is new or not. Based on where I need to do this in my app, I would like to do this by know if the user was created today or now.

How can I do something like:

if current_user.created_at.today?

Any timezone issues? Maybe it would be better to do, created_at in the last 24 hours?

Thanks

like image 647
AnApprentice Avatar asked Nov 28 '22 03:11

AnApprentice


2 Answers

I'd rather use current_user.created_at.to_date == Date.current, as it is more self explanatory.

like image 111
Pedro Nascimento Avatar answered Nov 30 '22 18:11

Pedro Nascimento


To check if the user was created in the last 24 hours, do something like this:

if current_user.created_at > Time.now - 24.hours
  #...
end
like image 44
Marek Příhoda Avatar answered Nov 30 '22 16:11

Marek Příhoda