我有一段時間間隔的虛擬屬性的模型:設置虛擬屬性
attr_accessible :description, :time_end, :time_start, :duration
belongs_to :timesheet
def duration
if attribute_present?("time_start") and attribute_present?("time_end")
ChronicDuration.output(self.time_end - self.time_start)
else
ChronicDuration.output(0)
end
end
def duration=(d)
self.time_end = self.time_start + d
end
但是,創建一個新的對象時,Rails的嘗試開始之前設置時間,從而導致錯誤。如何確保在開始後持續時間設置爲?
錯誤:
undefined method `+' for nil:NilClass
PARAMS:
{"utf8"=>"✓",
"authenticity_token"=>"dg+CysIxZORyV3cwvD+LdWckFdHgecGDFDBNOip+iKo=",
"entry"=>{"time_start"=>"now",
"duration"=>"2h",
"description"=>""},
"commit"=>"Create Entry"}
1)我同意是這樣的。實際名稱是time_end –