I have a problem in my database i have a table called members and have two fields start_date and end_date..fro开发者_高级运维m the form people can enter the start date and from there i want to add three months the start_date to give the end_date
e.g
@member.end_date = params[:start_date] + 3months
can somebody please help me with this..Am using Rails3 by the way thanks
Assuming params[:start_date]
is a Time
or Date
object, you've pretty much got it right with your example. You just need to use 3.months
rather than 3months
.
@member.end_date = params[:start_date] + 3.months
As an aside, this logic should probably live in the model (your code looks like you're doing this in the controller)...
class Member < ActiveRecord::Base
before_save :set_end_date
private
def set_end_date
self.end_date = self.start_date + 3.months
end
end
You're just missing a dot in your syntax.
@member.end_date = params[:start_date] + 3.months
Here's an example in irb (rails console):
$ rails c
Loading development environment (Rails 3.0.3)
> start_date = Time.now
=> 2011-01-29 17:18:58 +0000
> end_date = start_date + 3.months
=> 2011-04-29 17:18:58 +0100
精彩评论