I’m an amateur coder. Know enough to get into trouble, not enough to
get out of it.
I’m trying to make an office app for the photo studio I work for. Part
of this app is a scheduler.
Our assignments typically do not span more than a day.
I have defined starts_at and ends_at attributes in my model, both
I have a datetime_select helper in the form to establish starts_at.
Because year, month and day will be the same for ends_at, I would
prefer the user not have to enter that redundant information. Ideally
I would like the user to only need to specify hour and minute for
ends_at, and have the rest picked up from starts_at.
For that reason, I added two textfield_tag helpers to the form to
capture how many hours and how many minutes we anticipated the event
I understand that a before_save callback in the model is a smart way
to go. I can’t figure out how to do it.
Using raise params, I learned that the datetime_select helper does not
create a full blown datetime object but rather adds 5 items to the
params hash: starts_at(1i) - starts_at(5i)
I tried this (and multiple variations of this) to the before_save
class Event < ActiveRecord::Base
self.ends_at.year = params[:starts_at[:1i]]
self.ends_at.month = params[:starts_at[:2i]]
self.ends_at.day = params[:starts_at[:3i]]
self.ends_at.hour = params[:starts_at[:4i]] + params
self.ends_at.min = params[:starts_at[:5i]] + params
That was a mess, so, following a helpful tip, I tried
ends_at = ends_at.change(:year => starts_at.year, :month =>
:day => starts_at.day, :hour => (starts_at.hour + params
:min => (starts_at.min + params[:duration_minutes]))
I’m clearly botching the syntax for the callback because nothing I
have tried has worked.