Best way to create random DateTime in Rails

前端 未结 12 2022
粉色の甜心
粉色の甜心 2020-12-22 23:49

What is the best way to generate a random DateTime in Ruby/Rails? Trying to create a nice seeds.rb file. Going to use it like so:

 Foo.create(name: Faker::Lo         


        
12条回答
  •  清酒与你
    2020-12-23 00:15

    You can pass Time Range to rand

    rand(10.weeks.ago..1.day.ago)
    

    Output Example:

    => Fri, 10 Jan 2020 10:28:52 WIB +07:00
    

提交回复
热议问题