class Bus include Mongoid::Document include Mongoid::Timestamps field :bus_route, localize: true field :departure_time, type: DateTime field :reservation_end_time, type: DateTime field :remark, localize: true field :capacity, type: Integer field :created_by has_many :bookings def can_reserve? self.reservation_end_time > Time.now && self.bookings.count <= self.capacity end def posted_by User.find(self.created_by).name rescue "" end def already_reserved?(user) self.bookings.where(:user_id => user.id.to_s).count == 1 end end