Ruby on rails Rails:使用存储在数据库中的时间标识符“计算时间”;“字符串”;
我想这样计算未来的日期:Ruby on rails Rails:使用存储在数据库中的时间标识符“计算时间”;“字符串”;,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我想这样计算未来的日期: Date.today + 1.year Date.today + 1.month 为了确定未来日期是一个月还是一个年,我从数据库请求持续时间。持续时间值保存为字符串 end_date = Date.today + 1.Duration.find(order.duration_id).duration 我相信上述代码执行如下: Date.today + 1."year" #the quotes cause an error 如何删除引号?在rails中,1.year
Date.today + 1.year
Date.today + 1.month
为了确定未来日期是一个月
还是一个年
,我从数据库请求持续时间
。持续时间
值保存为字符串
end_date = Date.today + 1.Duration.find(order.duration_id).duration
我相信上述代码执行如下:
Date.today + 1."year" #the quotes cause an error
如何删除引号?在rails中,
1.year
的工作方式是:year
是1
上的一个实例方法。在ruby中,要计算对象上的任意方法,只需使用send('method\u name')
或send(:method\u name)
如果将持续时间设置为字符串,则可以使用:send
计算实际持续时间:
duration = "year"
1.send(duration) == 1.year