2013-04-10 27 views
0

您好,我正在爲用於輸入預訂房間的start_date和end_date的房間預訂開發演示應用程序。軌道日期範圍內的布爾值false

狀態是布爾類型。默認爲true。

我怎樣才能讓狀態只爲特定範圍之間的start_date和end_date。

爲了這個,我有兩個型號的房間和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 

+0

'BookingDetails'表中不應該有'status'嗎? – Zippie 2013-04-10 10:35:54

+0

房間表 – r15 2013-04-10 11:08:12

+1

中沒有狀態,但是如果您的某個房間是從start_date預訂到end_date的,那麼房間的全局值將爲false,而不僅僅是這些日期。 – Zippie 2013-04-10 11:09:25

回答

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 
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