r15
r15

Reputation: 496

boolean value false in date range for rails

Hello i am developing demo app for room booking where user enters their start_date and end_date for room booking.

status is boolean type. default is true.

how can i make status false only for particular range between start_date and end_date.

for this i have two models as Room and BookingDetail as follows

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

end

Upvotes: 0

Views: 111

Answers (2)

Zippie
Zippie

Reputation: 6088

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

Upvotes: 0

ted
ted

Reputation: 5329

If i got you correctly you may not need to persist status:

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

Upvotes: 1

Related Questions