Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 从Datamapper类中访问子级时遇到问题_Ruby_Datamapper_Ruby Datamapper - Fatal编程技术网

Ruby 从Datamapper类中访问子级时遇到问题

Ruby 从Datamapper类中访问子级时遇到问题,ruby,datamapper,ruby-datamapper,Ruby,Datamapper,Ruby Datamapper,试图通过在酒店的天数来计算酒店住宿的价格。问题在于“def计算价格”。价格是属于预订的空间类别的属性。在我的模型中,我可以调用Booking.space.price,并显示出它的价值,但我似乎无法从类本身(即使是self)中实现这一点。使用Datamapper可以实现这一点吗 class Booking include DataMapper::Resource attr_reader :calculate_stay, :calculate_price property :id,

试图通过在酒店的天数来计算酒店住宿的价格。问题在于“def计算价格”。价格是属于预订的空间类别的属性。在我的模型中,我可以调用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