比较Rails中的日期时间变量 [英] Comparing datetime variables in Rails
问题描述
Rails 3 Ruby 1.92 - 我有一个帮助方法来比较对象上的datetime属性与当前日期&时间。
Rails 3 Ruby 1.92 - i have a helper method comparing a datetime attribute on an object to the current date & time.
module StoreHelper
def initial_time(product)
if product.time_type == "Min"
initial_time = (product.time * 60)
elsif product.time_type == "Hrs"
initial_time = (product.time * 3600)
else
initial_time = 0
end
end
def time_based_price(product)
start_time = product.start_time
current_time = Time.now
expire_time = start_time + initial_time(product)
if (current_time <= expire_time) && (unit_sales(product)>= product.volume)
time_based_price = (product.price - product.boost_savings)
else
time_based_price = product.price
end
end
end
当我从我的视图中调用它时,我会收到错误undefined methodto_datetime '为0:Fixnum。如果我将比较行(current_time< = expire_time)更改为(expire_time> = current_time),我会收到错误Fixnum与Time failed的比较
when i call it from my view i get error "undefined method `to_datetime' for 0:Fixnum". if i change the comparison line (current_time <= expire_time)to (expire_time >= current_time) i get error"comparison of Fixnum with Time failed"
使用.now,.current,.zone.now为Time和DateTime,没有运气。当我从我的观点调用Time.zone.now和expire_time值时,它们具有相同的格式:
I've tried using .now, .current, .zone.now for both Time and DateTime with no luck. When i call the Time.zone.now and expire_time values from my view they have the same format:
2013-11-17 20:48:07 UTC - Time.zone.now
2013-11-17 16:15:00 UTC - expire_time
推荐答案
您是否在列表中为每个产品项目调用此方法?在这种情况下,看起来像一个产品,没有为start_time和time_type设置任何值。当expire_time为0时,您会收到此错误。但是,由于您已尝试过expire_time的值,所以我认为它必须发生在更多的产品实例上,并且对于其中一个实例而言它将失败。
Are you calling this method for each product item in a list? In that case, looks like for one of the product, no values have been set for start_time, and time_type. You would get this error when expire_time is 0. But since you have tried the value of expire_time, I believe it must be happening for more product instances and it fails for one of those.
这篇关于比较Rails中的日期时间变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!