1

Trying to calculate the price of a hotel stay by the days stayed at the hotel. The problem is in the "def calculate price". The price is an attribute of the Space class which belongs to Booking. In my model I can call Booking.space.price, and bring out the value, but I can't seem to do it from within the class itself (even with self). Is this possible with 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

0 Answers0