ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Combining two data columns/types into one #825

Closed atflick closed 7 years ago

atflick commented 7 years ago

I haven't been able to find a good answer or if this is even possible. But right now I am using this calendar gem that uses a 'datetime' data type to display the events on the calendar, labeled 'start_time' in the snippet of my model below. For controlling incoming data types and better UX I have split out the time and date columns. Basically I am trying to figure out if it's possible to populate the 'start_time' field with a combination of the two. I have a method for this but just not quite sure how to correctly implement it or if it's even possible?

create_table "reservations", force: :cascade do |t|
    t.date     "res_date"
    t.time     "res_time"
    t.datetime "start_time"
end

Thanks! -Andy

superbuggy commented 7 years ago

Yeah, start_time will just be 'expecting' a DateTime object. You can create a DateTime object passing whatever parameters you need. This is kind of an adapter-based solution.

superbuggy commented 7 years ago

You might be able to pass a Date object and a Time object as arguments to DateTime.new, also.

atflick commented 7 years ago

Andy helped me, I created a method in my model that combines the date and time and puts it in the column I need and just needed to update my controller as follows

  def create
    @reservation = Reservation.new(reservation_params)
    @reservation.combine_datetime
    @reservation.save
    redirect_to reservations_path
  end