0
我有attr_acessor模型订票:日期和时间:轨道4 attr_accessor和新记录
class Booking < ActiveRecord::Base
# ...
before_save :convert_date_and_time
attr_accessor :date, :time
def convert_date_and_time
self.date_and_time = DateTime.parse("#{date.to_s} #{time.to_s}")
end
end
我试图定义的日期和时间getter方法:
def date
date_and_time.to_date if self.id.present?
end
def time
date_and_time.to_time if self.id.present?
end
但我认为这不是完成它的方式。我需要self.id.present?因为当我试图创建新记录时,显然date_and_time仍然没有任何价值,并且getters会产生错误。
如果是这种情况,那么getters应该如何处理,以便我可以处理尚未保存的新记录?我应该像现在这样离开他们吗?
谢谢!