根据入住酒店的日期,尝试计算酒店住宿的价格。问题在于" def计算价格"。价格是Space类的属性,属于Booking。在我的模型中,我可以调用Booking.space.price,并显示值,但我似乎无法从类本身(即使使用self)中执行此操作。 Datamapper可以实现吗?
class Booking
include DataMapper::Resource
attr_reader :calculate_stay, :calculate_price
property :id, Serial
property :start_date, String
property :end_date, String
property :message, Text
property :guest_number, Integer
belongs_to :space
def calculate_stay
start_date = Date.parse(self.start_date)
end_date = Date.parse(self.end_date)
return (end_date - start_date)+1
end
def calculate_price
(self.calculate_stay).to_i * (self.space.price).to_i
end
end