2017-02-24 60 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'