1

I'm trying to change a 'time' entry with the date entered in the 'date' entry. So if the time is "2000-01-01 10:00:00 UTC" and the date is "2021-10-10" I want the output to be "2021-10-10 10:00:00 UTC".

I almost have it working, however; when I assign the updated date back to the original object, it does not save the change. For instance, in the code below, event_time contains the proper time I want, however, assigning it to @event.time and then printing @event.time shows the change did not take place.

def create
  @event = Event.new(event_params)
  event_date = @event.date
  event_time = @event.time.change(:year => event_date.year, :month => event_date.month, :day => event_date.day)

  puts event_time              # prints 2021-10-22 06:06:00 UTC
  @event.time = event_time
  puts @event.time             # prints 2000-01-01 06:06:00 UTC

  if @event.save
    redirect_to(events_path)
  else
    render('new')
  end
end

Any suggestions? I'm new to Ruby so I'm probably missing something obvious here

Here's my schema

  create_table "events", force: :cascade do |t|
    t.date "date"
    t.string "description"
    t.boolean "isMandatory"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "name"
    t.time "time"
    t.string "location"
  end
4
  • so if you put another puts statement in the if clause right under the if @event.save the time changes back? Can you share your DB schema for this table? Commented Oct 10, 2021 at 21:09
  • Yes, it would change back, if I put it under the if. It changed back even before that though, after the 1st puts statement I have. I have added my schema to the post Commented Oct 10, 2021 at 21:28
  • 1
    Is there a particular reason you're setting the date and time as separate fields? Can you not just store a single value with the event datetime? also, it's generally always best practice to make sure that columns are not reserved words, or types, as you'll find only headaches in that direction Commented Oct 10, 2021 at 22:13
  • Does if @event.save actually return successful? Happens a redirect after running this method or is the new page rendered again? Commented Oct 11, 2021 at 6:05

1 Answer 1

2

You can refer to the SO answer here

The problem is that there is no time-of-day class in Ruby or Rails. All the time classes are dates or timestamps (i.e. date plus time of day). Inside the database it will be a time (without timezone) column and it will behave properly inside the database. However, once the time gets into Ruby, ActiveRecord will add a date component because there is no plain time-of-day class available, it just happens to use 2000-01-01 as the date. Everything will be fine inside the database but you'll have to exercise a little bit of caution to ignore the date component when you're outside the database in Rails.

Use datetime column type to hold a date and time. Only use time in the migration if you don't need the date (only want to store time part).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.