-
-
Save ryanjm/2320468 to your computer and use it in GitHub Desktop.
# == 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 |
I'm sorry, I've been pretty busy lately. Did you ever get this figured out?
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)
Here's the basics of my showtime model now:
In case it is required, here's the basics/relevant parts of the Performance model:
Here's the failing test:
Hopefully I've not broken anything in the above code in my attempt to just show the necessary parts.