jackc / tod

Time of day and shift types for Ruby
MIT License
435 stars 56 forks source link

Can't make it work on rails 5.0.0 #57

Closed bceppi closed 6 years ago

bceppi commented 6 years ago

Hello,

Thank you so much for developing this gem, it will help me a lot. The problem is that I can't make it work. I'm currently working with ruby 2.3.3 and rails 5.0.0.

I have an Event model with start_time and end_time, that are postgresql's time attributes.

In the migration file:

class CreateEvents < ActiveRecord::Migration[5.0]
  def change
    create_table :events do |t|
      ...
      t.time :start_time
      t.time :end_time
      t.timestamps
    end
  end
end

In event.rb:

class Event < ApplicationRecord
    serialize :start_time, Tod::TimeOfDay
    serialize :end_time, Tod::TimeOfDay

In application.rb:

  config.active_record.time_zone_aware_types = [:datetime]

Finally, when I try to create one I get:

init(main)> event = Event.new(start_date: Date.today, start_time: Tod::TimeOfDay.new(9,30), end_time: Tod::TimeOfDay.new(20,30), full_day_event: false, recurrent: false)
#<Event:0x007f81c03a3b00> {
        :id => nil,
        :start_date => Wed, 05 Sep 2018,
        :start_time => #<Tod::TimeOfDay:0x007f81c03a1378 @hour=9, @minute=30, @second=0, @second_of_day=34200>,
        :end_time => #<Tod::TimeOfDay:0x007f81bf9d4548 @hour=20, @minute=30, @second=0, @second_of_day=73800>,
        :full_day_event => false,
        :recurrent => false,
        :created_at => nil,
        :updated_at => nil
}

init(main)> event.save
   (0.2ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "events" ("start_date", "full_day_event", "recurrent", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5,) RETURNING "id"  [["start_date", "2018-09-05"], ["full_day_event", "f"], ["recurrent", "f"], ["created_at", "2018-09-05 20:11:06.345258"], ["updated_at", "2018-09-05 20:11:06.345258"]]
   (3.2ms)  COMMIT
true

init(main)> event
#<Event:0x007f81c03a3b00> {
        :id => 14,
        :start_date => Wed, 05 Sep 2018,
        :start_time => nil,
        :end_time => nil,
        :full_day_event => false,
        :recurrent => false,
        :created_at => Wed, 05 Sep 2018 17:11:06 -03 -03:00,
        :updated_at => Wed, 05 Sep 2018 17:11:06 -03 -03:00
}

So neither the start_time and end_time are considered on the SQL query and are not saved in the database without showing any errors.

Is there anything I'm doing wrong?