我的Rails应用程序中有一组地图,属于酒店。我希望使用状态过滤所有地图。这是使用以下范围工作:
scope :status, -> (status) {where status: status}
我希望排除属于状态为X的酒店的所有地图。这是我尝试过的,但我没有成功。我在酒店模型内有以下范围:
scope :exclude, -> (status) {where.not(status: status)}
这就是我在Map模型中尝试的:
scope :status, -> (status) {where(status: status) && Hotel.exclude(1) }
我不确定,但我认为你可以做一些类似的事情:
class Map < ActiveRecord::Base
scope :exclude, -> (status) {
joins(:hotel).where(Hotel.arel_table[:status].not_eq(status))
}
end
我不确定你们的关系,但试试:
Map.status(map_status).joins(:hotel).merge(Hotel.exclude(hotel_status))
在地图中:
scope :choose_name, ->(map_status, hotel_status) { status(map_status).joins(:hotel).merge(Hotel.exclude(hotel_status)) }
然后Map.choose_name(map_status, hotel_status)