2017-02-24 59 views
1

我正在构建预订系统,用户可以在其中搜索公开预订。预订有一个范围,用于过滤在给定日期和时间预订的预订。此预订过滤功能可以正常工作,但我也希望为给定的seating_capacity(宾客)在表格模型中添加过滤器,以便在给定日期/时间内显示与给定seating_capacity相匹配的所有预订预订。任何想法如何添加该过滤器?两种型号的有效记录筛选器

模型

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 

控制器

class TablesController < ApplicationController 
    def index 
    @reserved_tables = Table.reserved_on(params[:guests], params[:day], params[:time]) 
    @open_tables = Table.free_on(params[:guests], params[:day], params[:time]) 
    end 
end 

查看

<%= form_tag(tables_path, :method => "get", id: "table-search-form") do %> 
    <%= text_field_tag :day, params[:day], class:"datepicker", placeholder: "Select Day" %> 
    <%= text_field_tag :time, params[:time], class:"timepicker", placeholder: "Select Time" %> 
    <%= submit_tag "Search", :name => nil %> 
<% end %> 

架构

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 
+0

你有没有跟你接触过的方式什么问题?知道具体问题是什么会更容易。 – Nobita

+0

我可以根据':date'和':starts_at'和':ends_at'次来过滤保留。但我想添加一个基于他们的':seating_capacity'过滤表的作用域,并将其链接到预留:在过滤器上。通过这种方式,我将只在选定的日期/时间获得> ='seating_capacity'的表格。 – rymcmahon

回答

1

可以实现使用joins(:table)的seating_capacity范围,这是它的外观:

scope :with, -> (capacity) { joins(:table).where(tables: {seating_capacity: capacity}) } 

然后你就可以查询当天具体的保留,time和seating_capacity是这样的:

Reservations.on(day, time).with(capacity) 
+0

在这个查询中,':with'作用域返回所有'Reservation'和''capacity'',但是您可以对'seating_capacity'做更大的作用。这取决于你想达到什么。 –

+0

我试过了,但表格仍然没有过滤'seating_capacity'。当查询包含'(seating_capacity> ='4')'时,将返回所有表格,包括座位容量为2的表格。 – rymcmahon

+0

这是因为使用的语法不正确。试试这个:'join(:tables).where(“tables.seating_capacity> =?”,容量)' –

0
@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'