# Stumped by scheduler

**URL:** <https://www.ruby-forum.com/t/stumped-by-scheduler/176568>\
**Category:** Rails\
**Created:** [October 2, 2009, 9:49am UTC](https://www.ruby-forum.com/t/stumped-by-scheduler/176568 "2009-10-02T09:49:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snlsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@snlsn](https://www.ruby-forum.com/u/snlsn)\
**Post date:** [October 2, 2009, 9:49am UTC](https://www.ruby-forum.com/t/stumped-by-scheduler/176568/1 "2009-10-02T09:49:40Z")

</div>

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  
datetime objects.

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  
to last.

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  
method:

class Event \< ActiveRecord::Base  
before\_save :create\_end  
def create\_end  
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  
[:duration\_hours]  
self.ends\_at.min = params[:starts\_at[:5i]] + params  
[:duration\_minutes]  
end  
end

That was a mess, so, following a helpful tip, I tried

def create\_end  
ends\_at = ends\_at.change(:year =\> starts\_at.year, :month =\>  
starts\_at.month,  
:day =\> starts\_at.day, :hour =\> (starts\_at.hour + params  
[:duration\_hours]),  
:min =\> (starts\_at.min + params[:duration\_minutes]))  
end

I’m clearly botching the syntax for the callback because nothing I  
have tried has worked.

Help?  
Thanks.

Steve

---

<div class="post-metadata">

**Author:** ![snlsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@snlsn](https://www.ruby-forum.com/u/snlsn)\
**Post date:** [October 2, 2009, 10:03am UTC](https://www.ruby-forum.com/t/stumped-by-scheduler/176568/2 "2009-10-02T10:03:08Z")

</div>

On Oct 1, 7:19 pm, SNelson [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Well there is some funkiness todo with how time parameters are passed  
around, but that is not the problem here. Equally the components of a  
date or time object are readonly properties. What I think the core  
misconception here is that params is a controller method therefore you  
cannot call it from a model instance method. Usually you don’t need  
to: why fiddle with params[:starts\_at] when you already have  
self.starts\_at ? I’m not sure a callback is right for you here, are  
you always going to want to update ends\_at even on subsequent saves ?  
You could instead have a method that takes a number of hours and  
minutes and sets tends at correspondlingly, call that from your  
controller. Lastly be careful about your calculations - not sureyour  
examples wouldn’t work if starts\_at hour was 23 and duration\_hours was  
3 for example)

Fred

---

<div class="post-metadata">

**Author:** ![snlsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@snlsn](https://www.ruby-forum.com/u/snlsn)\
**Post date:** [October 2, 2009, 2:41pm UTC](https://www.ruby-forum.com/t/stumped-by-scheduler/176568/3 "2009-10-02T14:41:13Z")

</div>

Thanks so much for taking the time to consider this Fred.

I got it working, thanks to some patient help from several complete  
strangers. The generosity inside online communities is a wonderful  
thing.

I had to add both duration\_minutes and duration\_hours to the model so  
the callback could access them. I had initially been thinking of them  
as just “disposable” items, not worth saving. Now I’m glad I did  
because, to answer your question, yes we are just as likely to change  
the duration of an assignment as we are to change its start time. Lots  
of little last minute changes in our shop.  
☹  
Once those attributes became available to the model, I could write a  
before\_save callback like this:

def create\_end  
self.ends\_at = starts\_at.advance(:hours =\> duration\_hours, :minutes  
=\> duration\_minutes)  
end

So thanks again for your help. I learned some interesting things. Not  
having worked with model callback methods before, I had not grasped  
that the params method was not available to models. Thanks for that  
insight.

Steve
