# Using time.now.advance

**URL:** <https://www.ruby-forum.com/t/using-time-now-advance/146502>\
**Category:** Rails\
**Created:** [August 21, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/using-time-now-advance/146502 "2008-08-21T19:33:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [August 21, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/using-time-now-advance/146502/1 "2008-08-21T19:33:39Z")

</div>

Hi,

I’m building somewhat of a billing system which needs to set next  
renewal dates for each account package. In my account\_package model I  
have this:

class AccountPackage \< ActiveRecord::Base  
belongs\_to :account  
belongs\_to :package  
belongs\_to :package\_cycle

before\_save :set\_renewal\_date

def set\_renewal\_date  
next\_renewal\_date =  
Time.now.advance(self.package\_cycle.cycle.frequency)  
self.next\_renewal = next\_renewal\_date  
end  
end

Basically, my Cycle model has a frequency column where I’ve stored the  
strings :months =\> 1, :years =\> 1, :years =\> 2, etc., hoping I could  
just use the value in the database within the Time.now.advance()  
method. It doesn’t work. Each time the record saves, I just get the  
value of Time.now in there.

Any ideas what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [August 21, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/using-time-now-advance/146502/2 "2008-08-21T19:52:24Z")

</div>

On 21 Aug 2008, at 18:32, Jeremy wrote:

> Basically, my Cycle model has a frequency column where I’ve stored the  
> strings :months =\> 1, :years =\> 1, :years =\> 2, etc., hoping I could  
> just use the value in the database within the Time.now.advance()  
> method. It doesn’t work. Each time the record saves, I just get the  
> value of Time.now in there.

You can’t pass a string, you need to pass a hash. One way of doing  
that would be to use the activerecord’s serialize method so that the  
Cycle model would actually store the hash

Fred

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [August 21, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/using-time-now-advance/146502/3 "2008-08-21T20:00:36Z")

</div>

Thanks, Fred. That makes sense. I’m not getting the following:

undefined method `[]’ for :“months =\> 1”:Symbol

What am I doing now?

On Aug 21, 1:50 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [August 21, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/using-time-now-advance/146502/4 "2008-08-21T20:21:05Z")

</div>

On 21 Aug 2008, at 18:59, Jeremy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks, Fred. That makes sense. I’m not getting the following:
> 
> undefined method `[]’ for :“months =\> 1”:Symbol

It’s expecting a hash that it can do [] on but you appear to be  
passing a symbol

Fred

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [August 21, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/using-time-now-advance/146502/5 "2008-08-21T21:05:41Z")

</div>

I found a way to do it…

I added a frequency\_unit column to my Cycles model. Now I store the  
string “years” or “months” or whatever in frequency\_unit and store 1,  
2, 3, etc. in frequency. I can then use this code, which works the  
way I want it to:

before\_save :set\_renewal\_date

def set\_renewal\_date  
next\_renewal\_date =  
Time.now.advance(self.package\_cycle.cycle.frequency\_unit.to\_sym =\>  
self.package\_cycle.cycle.frequency.to\_i)  
self.next\_renewal = next\_renewal\_date  
end

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [August 21, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/using-time-now-advance/146502/6 "2008-08-21T20:45:44Z")

</div>

So I have a text field on a page that I enter in something for that  
frequency. I now have the model set to serialize :frequency. What do  
I need to type into the text field so that this will work? Sorry to  
be a bother, just at a loss and can’t figure it out…

On Aug 21, 2:20 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
