我正在建立一个预订系统,用户可以搜索一个公开预订。 :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 endController:
class TablesController < ApplicationController def index @reserved_tables = Table.reserved_on(params[:day], params[:time]) @open_tables = Table.free_on(params[:day], params[:time]) end endView:
<%= 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 endSeems 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 endOr 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更多推荐
发布评论