这是我的代码。这是contact.rb的一种方法(联系人是模型):
def event_delay event
# find instance of contact_event
puts "+++++++++++++++inside event_delay method"
event_class = event.class.name
event_id = event_class.foreign_key.to_sym
#puts event_id
contact_event_class = "Contact#{event_class}".constantize
#puts contact_event_class
contact_event = contact_event_class.first(:conditions =>
{:contact_id => self.id,
event_id => event.id})
#puts "inspect contact_event"
#puts contact_event.inspect
if !contact_event.nil?
puts "---- #{contact_event.title} not nill"
puts contact_event.id
date_sent = contact_event.date_sent
target_send_date = self.date_entered + event.days
puts "date_sent:"
puts date_sent.to_date
puts "target send date:"
puts target_send_date
return date_sent - target_send_date
end
end
目标是返回date_sent与target_send_date之间的天数差异。但是我收到以下错误:
undefined method `-@' for Sun, 08 Aug 2010:Date
答案 0 :(得分:3)
也许两个值都不是日期?定义了-
方法,这就是为什么我认为你可能没有两个日期。这对你有效吗?
irb(main):001:0> require "date"
=> true
irb(main):002:0> Date.new(2010, 10, 20) - Date.new(2010, 9, 20)
=> Rational(30, 1)
irb(main):003:0>
答案 1 :(得分:2)
迁移到Rails 3.2后,我在运行规范时遇到了同样的奇怪错误,但能够将其隔离出来:
3.days.ago - Date.today
=> NoMethodError: undefined method `-@' for Sun, 29 Apr 2012:Date
'3.days.ago'生成一个ActiveSupport :: TimeWithZone对象,它(显然)不会与Date.today给你的Date对象混合。