Home>
I want to achieve

The code below generates 18 data at once, but if you look at it in seconds or less, there is a slight time difference. I want to prepare created_at seconds or less so that it will be used in later group processing.

#controller
[* 1..18] .each do | num |
    Score.create! (course: params [: course], hole_number: num, user_id: current_user.id, round_id: round_id, hole_score: nil, created_at: Time.now)
end


I tried it as created_at: Time.now, but it still shifts as shown in the image below.

Is there any way? Thank you.

  • Answer # 1

    I also want to have created_at seconds or less.

    registered_time = Time.now
    [* 1..18] .each do | num |
        Score.create! (course: params [: course], hole_number: num, user_id: current_user.id, round_id: round_id, hole_score: nil, created_at: registered_time)
    end