How can I add two weeks to the current Time.now in Ruby? I have a small Sinatra project that uses DataMapper and before saving, I have a field populated with the current time PLUS two weeks, but is not working as needed. Any help is greatly appreciated! I get the following error:
NoMethodError at /
undefined method `weeks' for 2:Fixnum
Here is the code for the Model:
class Job
include DataMapper::Resource
property :id, Serial
property :position, String
property :location, String
property :email, String
property :phone, String
property :description, Text
property :expires_on, Date
property :status, Boolean
property :created_on, DateTime
property :updated_at, DateTime
before :save do
t = Time.now
self.expires_on = t + 2.week
self.status = '0'
end
end
I think
week/weeks
is defined in the active support numeric extensionRuby
Date
class has methods to add days and months in addition to seconds in Time. An example:You can use these 3 patterns
You don't have such nice helpers in plain Ruby. You can add seconds:
But, fortunately, there are many date helper libraries out there (or build your own ;) )