2013-04-10 81 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