Ruby on rails 两种型号的活动记录过滤器
我正在建立一个预订系统,用户可以在其中搜索开放预订。预订具有:on作用域,用于过滤在给定日期和时间预订的预订。仅在预订上使用此过滤器就可以了,但我还想为给定的Ruby on rails 两种型号的活动记录过滤器,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我正在建立一个预订系统,用户可以在其中搜索开放预订。预订具有:on作用域,用于过滤在给定日期和时间预订的预订。仅在预订上使用此过滤器就可以了,但我还想为给定的座位容量(来宾)在表模型中添加一个过滤器,这样我就可以在给定的日期/时间显示与给定的座位容量匹配的所有预订。你知道如何添加这个过滤器吗 模型 class Reservation < ApplicationRecord belongs_to :user, optional: true belongs_to :table, opt
座位容量
(来宾)在表模型中添加一个过滤器,这样我就可以在给定的日期/时间显示与给定的座位容量
匹配的所有预订。你知道如何添加这个过滤器吗
模型
class Reservation < ApplicationRecord
belongs_to :user, optional: true
belongs_to :table, optional: true
scope :on, -> (day, time) { where('date = ? AND starts_at <= ? AND ends_at > ?', day, time, time)}
end
class Table < ApplicationRecord
has_many :reservations
has_many :users, through: :reservations
def self.free_on(guests, day, time)
reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id')
reserved_table_ids ? where.not(id: reserved_table_ids) : all
end
def self.reserved_on(guests, day, time)
reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id')
where(id: reserved_table_ids)
end
end
您需要使用join,这里给出了一个很好的示例
您可以使用
连接(:table)
实现座位容量范围,如下所示:
scope :with, -> (capacity) { joins(:table).where(tables: {seating_capacity: capacity}) }
然后,您可以查询具体的预订日期、时间和座位,如下所示:
Reservations.on(day, time).with(capacity)
Datagrid是很好的在页面上轻松添加过滤器的工具
结帐演示:您的处理方式有什么问题?更容易知道具体的问题是什么。我可以根据
:日期和:开始时间和:结束时间来过滤预订。但是我想添加一个作用域,根据表的:座位容量
对表进行过滤,并将其链接到reservation:on filter。通过这种方式,我将只获得在所选日期/时间的座位数>=的表。在这个查询中,:with
范围返回所有的预订
,并具有确切的座位数
,但您也可以对更大的座位数
执行相同的操作。这取决于你想要实现什么。我试过了,但桌子仍然没有根据座位容量进行过滤。当查询包含(座位数>='4')
时,将返回所有表格,包括座位数为2的表格。这是因为使用的语法不正确。试试这个:连接(:tables)。其中(“tables.seatment\u capacity>=?”,capacity)
:tables
不会在预订时连接,因为属于
关系<代码>:table
将加入,但我在(table.seatching\u capacity)
处得到一个PG::SyntaxError
。我不能在上面调用座位容量
…你完全正确,我误读了db:schema!尝试使用联接(:table)
和(“tables.seatch\u capacity>=?”,capacity)
。表-在where()
中使用复数形式。我相当肯定这应该行得通。如果有,我会更新我的答案。
create_table "reservations", force: :cascade do |t|
t.integer "user_id"
t.integer "table_id"
t.datetime "date"
t.time "starts_at"
t.time "ends_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["table_id"], name: "index_reservations_on_table_id", using: :btree
t.index ["user_id"], name: "index_reservations_on_user_id", using: :btree
end
create_table "tables", force: :cascade do |t|
t.integer "seating_capacity"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
scope :with, -> (capacity) { joins(:table).where(tables: {seating_capacity: capacity}) }
Reservations.on(day, time).with(capacity)
@reservationsWithinInterval1 = Reservation.where('startdate >= ? AND startdate < ? , @reservation.startdate, @reservation.enddate)
@reservationsWithinInterval2 = Reservation.where('enddate >= ? AND enddate < ?, @reservation.startdate, @reservation.enddate)
if @reservationsWithinInterval1.count > 0 || @reservationsWithinInterval2.count>0
flash[:notice] = 'Select different time. Car already reserved in this time'