我有两个型号,房间和布局。
Layout has_many :rooms
Room belongs_to :layout
房间里有吸烟的布尔值。布局有占用数量。
在Room类中,我想要一个基于smoking和occupancy_count变量返回房间列表的方法。例如,我想搜索可容纳至少4人的吸烟室,或者正好容纳2人的非吸烟房等。
然而,我不能为我的生活弄明白。
在房间模型中,我试过......
def self.rooms_with_occupancy_and_smoking(occupancy_count, smoking)
layouts = Layout.layouts_with_occupancy_count_gt(occupancy_count) //method self explanatory
Room.rooms_with_smoking(smoking).joins(layouts) //method self explanatory
end
然而,我得到了一个
Runtime Error: Unknown Class : Layout (specifically when running the second line in that method)
我该如何正确地做这样的事情?
=========型号============
class Layout < ActiveRecord::Base
has_many :rooms
validates :description, presence: true
validates :occupancy_count, presence: true
def self.layouts_with_occupancy_count(occupancy_count)
self.where('occupancy_count = ?', occupancy_count)
end
def self.layouts_with_occupancy_count_gt(occupancy_count)
self.where('occupancy_count >= ?', occupancy_count)
end
def self.rooms_with_layout(layout_id)
self.where(id: layout_id).rooms
end
end
class Room < ActiveRecord::Base
belongs_to :hotel
belongs_to :layout
has_many :visits
validates :number, presence: true
validates :rate, presence: true
#validates :smoking, presence: true
def self.rooms_with_smoking(smoking)
self.where('smoking = ?', smoking)
end
def self.occupied_rooms(from_date, to_date)
self.joins(:visits).where('date >= ? and date <= ?', from_date, to_date).uniq
end
def self.vacant_rooms(from_date, to_date)
self.where.not(id: Room.occupied_rooms(from_date, to_date))
end
def self.find_rooms_with(occupancy_count, smoking, from_date, to_date)
Room.vacant_rooms(from_date, to_date).joins(:layout).where('occupancy_count >= ?', occupancy_count).where('smoking = ?', smoking)
end
def self.rooms_with_occupancy_and_smoking(occupancy_count, smoking)
layouts = Layout.layouts_with_occupancy_count_gt(occupancy_count) //method self explanatory
Room.rooms_with_smoking(smoking).joins(layouts) //method self explanatory
end
def self.blahblah(occupancy_count, smoking, from_date, to_date)
layouts = Layout.layouts_with_occupancy_count_gt(occupancy_count)
Room.rooms_with_smoking(smoking).vacant_rooms(from_date, to_date).joins(layouts)
end
def self.lowest_room_rate(hotel_id)
self.where('hotel_id = ?', hotel_id).select(:rate).order(:rate).first
end
def self.highest_room_rate(hotel_id)
self.where('hotel_id = ?', hotel_id).select(:rate).order(:rate).last
end
end