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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> Any ideas what I''m doing wrong? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > > Any ideas what I''m doing wrong?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 21 Aug 2008, at 18:59, Jeremy <jeremy.bise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> What am I doing now? > > On Aug 21, 1:50 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> 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 >> >>> Any ideas what I''m doing wrong? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 21 Aug 2008, at 18:59, Jeremy <jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > What am I doing now? > > > On Aug 21, 1:50 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> 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 > > >>> Any ideas what I''m doing wrong?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---