Using time.now.advance

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?

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

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

undefined method `[]' for :"months => 1":Symbol

What am I doing now?

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

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

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