Skip to content

Instantly share code, notes, and snippets.

@JonMidhir
Created March 28, 2014 12:31
Show Gist options
  • Save JonMidhir/9831651 to your computer and use it in GitHub Desktop.
Save JonMidhir/9831651 to your computer and use it in GitHub Desktop.
Setting time attributes using Chronic to parse human text input.
class Shift < ActiveRecord::Base
validates :starts_at, presence: true
def parsable_start_time
starts_at.strftime("%-I.%M%P") if starts_at
end
def parsable_start_time=(time_string)
self.starts_at = Chronic.parse(time_string)
end
end
class ShiftsController < ApplicationController
# ...
def shift_params
params
.require(:shift)
.permit :parsable_start_time #, ...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment