0

您好,我正在开发用于房间预订的演示应用程序,用户输入他们的 start_date 和 end_date 进行房间预订。

状态是布尔类型。默认为真。

我如何才能使状态仅在 start_date 和 end_date 之间的特定范围内为假。

为此,我有两个模型作为 Room 和 BookingDetail,如下所示

class BookingDetail < ActiveRecord::Base

    belongs_to :room
    belongs_to :cart
    attr_accessible :member_type_id, :room_type_id, :start_date, :end_date, :room_rate_id, :room_no, :customer_id, :room_id    
end

class Room < ActiveRecord::Base      
  belongs_to :location
  has_many :room_rate
  belongs_to :room_type
  has_many :booking_details
  belongs_to :customer
  attr_accessible :room_no, :status , :location_id , :room_type_id, :room_rate_id, :start_date, :end_date

结尾

4

2 回答 2

1

如果我正确理解了您,您可能不需要保留状态:

class Room
  def status
    booking_details = self.booking_details
    periods         = []
    time_now        = Time.now

    booking_details.each{|bd| periods << [bd.start_date, bd.end_date]}
    periods.each{|period| return false if time_now.between? *period} 

    return true
  end
end
于 2013-04-10T11:18:02.147 回答
0
class BookingDetail < ActiveRecord::Base
    after_create :change_room_status 

    belongs_to :room
    belongs_to :cart
    attr_accessible :member_type_id, :room_type_id, :start_date, :end_date, :room_rate_id, :room_no, :customer_id, :room_id    

    def change_room_status
       if self.start_date > some_date &&  self.end_date < some_other_date
           self.room.status = false
           self.room.save!
       end
    end
end
于 2013-04-10T11:16:55.987 回答