Thanks for your help everyone.

I ended up using Chronic Duration gem.

https://github.com/hpoydar/chronic_duration


On Sunday, September 22, 2013 3:58:58 PM UTC+8, Colin Law wrote:
>
> On 20 September 2013 08:30, Fai Wong <wong...@gmail.com <javascript:>> 
> wrote: 
> > If I store 1.month as a string in the database, how do I convert 
> "1.month" 
> > into 1.month? 
> > 
> > This way I can use the "1.month" value stored in database to perform the 
> > following calculation. 
> > 
> > Time.now + 1.month 
>
> If you really feel you need to do this then you can store "1.month" as 
> a string and use eval 
>
> 1.9.3p194 :007 > delta = "1.month" 
>  => "1.month" 
> 1.9.3p194 :008 > Time.now 
>  => 2013-09-22 08:52:13 +0100 
> 1.9.3p194 :009 > Time.now + eval( delta ) 
>  => 2013-10-22 08:52:23 +0100 
>
> However if you do this be /very/ careful about what can get into the 
> database as arbitrary code can be executed using eval and conceivably 
> your machine could be hacked.  I DO NOT recommend that you do this. 
> If your requirement is for a delta with values such as 1 month 3 weeks 
> and so on, then I suggest having two fields, one for the quantity and 
> an enumerated value for the period (month, week and so on).  Then work 
> it out in code.  Less efficient but /much/ safer. 
>
> Colin 
>
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "Ruby on Rails: Talk" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to rubyonrails-ta...@googlegroups.com <javascript:>. 
> > To post to this group, send email to 
> > rubyonra...@googlegroups.com<javascript:>. 
>
> > To view this discussion on the web visit 
> > 
> https://groups.google.com/d/msgid/rubyonrails-talk/a19f9925-f725-4cd5-9dfd-6c8c4b897bd6%40googlegroups.com.
>  
>
> > For more options, visit https://groups.google.com/groups/opt_out. 
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/9273b7f1-c2bc-4776-9ab1-25f77341af30%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to