轨道预订系统(Rails Reservation System)

编程入门 行业动态 更新时间:2024-10-24 18:23:14
轨道预订系统(Rails Reservation System)

我正在建立一个预订系统,用户可以搜索一个公开预订。 :on范围过滤给定日期和时间的reserved_table_ids并创建一个reserved_table_ids数组。 如果在选定的日期/时间有保留, @reserved_tables和@open_tables包含正确的表格ID。 但是,如果在选定的日期/时间内没有任何预订,则reserved_table_ids为空,且@reserved_tables和@open_tables为空。 有关如何将所有table_ids铲入@open_tables如果reserved_table_ids为空的任何想法? 或者我应该考虑其他方法吗? (Rails 5.0.1)

楷模:

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(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') where.not(id: reserved_table_ids) end def self.reserved_on(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') where(id: reserved_table_ids) end end class User < ApplicationRecord has_many :reservations has_many :tables, through: :reservations end

控制器:

class TablesController < ApplicationController def index @reserved_tables = Table.reserved_on(params[:day], params[:time]) @open_tables = Table.free_on(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 %>

I am building a reservation system where users can search for an open reservation. The :on scope filters the reservations that are present on a given day and time and I create an array of reserved_table_ids. If there are reservations on the selected day/time, @reserved_tables and @open_tables contain the correct table ids. However, if there aren't any reservations on a selected day/time, reserved_table_ids is null and @reserved_tables and @open_tables are empty. Any ideas on how to shovel all table_ids into @open_tables if reserved_table_ids is null? Or are there other approaches I should consider? (Rails 5.0.1)

Models:

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(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') where.not(id: reserved_table_ids) end def self.reserved_on(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') where(id: reserved_table_ids) end end class User < ApplicationRecord has_many :reservations has_many :tables, through: :reservations end

Controller:

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

View:

<%= 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 %>

最满意答案

似乎有一个问题,当reserved_table_ids是空的。 你可以尝试添加一个额外的条件?

def self.free_on(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') if reserved_table_ids where.not(id: reserved_table_ids) else all end end

或者使用三元组:

def self.free_on(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') reserved_table_ids ? where.not(id: reserved_table_ids) : all end

Seems like there is an issue when reserved_table_ids is empty. Could you try adding an extra conditional?

def self.free_on(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') if reserved_table_ids where.not(id: reserved_table_ids) else all end end

Or using a ternary:

def self.free_on(day, time) reserved_table_ids = Reservation.on(day, time).pluck('DISTINCT table_id') reserved_table_ids ? where.not(id: reserved_table_ids) : all end

更多推荐

本文发布于:2023-07-24 20:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:轨道   系统   Rails   Reservation   System

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!