rails的日期范围内的布尔值false

时间:2013-04-10 10:29:44

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

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

status是布尔类型。默认为真。

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

为此我有两个型号如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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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