Created
April 6, 2012 14:41
-
-
Save ryanjm/2320468 to your computer and use it in GitHub Desktop.
Special variables for dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: showings | |
# | |
# id :integer not null, primary key | |
# start_time :datetime | |
# end_time :datetime | |
class Showing < ActiveRecord::Base | |
attr_accessor :showing_date, :start_date, :end_date | |
validates :showing_date, :start_date, :end_date, :presence => true | |
before_save :set_dates | |
# used to set the instance variables for editing the form | |
after_find :set_times | |
def set_times | |
self.showing_date = self.start_time.strftime("%A, %B %d, %Y") unless self.start_time.nil? | |
self.start_date = self.start_time.strftime("%-I:%M%p") unless self.start_time.nil? | |
self.end_date = self.end_time.strftime("%-I:%M%p") unless self.end_time.nil? | |
end | |
private | |
def set_dates | |
self.start_time = DateTime.strptime("#{start_date} #{showing_date} #{Time.now.zone}", "%I:%M%p %A, %B %d, %Y %z") | |
self.end_time = DateTime.strptime("#{end_date} #{showing_date} #{Time.now.zone}", "%I:%M%p %A, %B %d, %Y %z") | |
end | |
end |
Hey, no not yet.
I keep adding updates to my SO post (http://stackoverflow.com/questions/9955977/validate-date-and-time-fields-together-in-rails-model/) but no joy yet.
I'm starting to give up on the idea of seperate date and time fields so I've started converting my app to use a single datetime field/db column to see if that is a better option for me.
I'm now on the lookout for a datetime picker since I need the datetime to be in a textfield but I don't think the date part and time part can be split easily. Before I was using a datepicker on a textfield for the date and dropdowns for the time.
In the end I used a merged datetime text_field
and the code here: codegram/date_validator#25 (comment)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm sorry, I've been pretty busy lately. Did you ever get this figured out?