ruby-on-railsactiverecordsinatrasinatra-activerecord

ActiveRecord/Sinatra: separate columns for date and time?


How can I get form parameters from the user for date and time separately?

For example:

class CreatePosts < ActiveRecord::Migration
  create_table :posts do |t|
    t.string :title
    t.text :content
    t.date :date
    t.time :time
  end
end

I am aware that t.date is the correct syntax, however is t.time correct? I want to take the params for date and time and interpret them with consistency. For example, I always want 01/02/2015 to display as January, 2nd, 2015 and 13:00 to display as 1PM. I am aware I can take the date and manipulate it with @post.date.strftime('%A, %b %d, %Y') but can I do something similar with time?


Solution

  • Yes there is a time type. But you're thinking about the problem wrong.

    You should select the database type based on the level of precision that you need to store the timestamp. Not by how you intend to present it.

    Thats what helper methods are for.