Rails显示直通关系的结果



所以现在我显示一个TrainingEvents Index页面,其中包含每个位置的所有事件。我想做的是创建一个专门针对该特定区域的TrainingEvents索引页面。

我的模型看起来像:

class TrainingEvents
belongs_to :location, optional: true
class Locations
belongs_to :region, optional: true
belongs_to :user, optional: true
has_many :careers, dependent: :destroy
has_many :training_events, dependent: :destroy
class Regions
has_many :locations, dependent: :destroy
has_many :zipcodes, dependent: :destroy
has_many :careers, through: :locations
has_many :training_events, through: :locations
has_and_belongs_to_many :logos
has_and_belongs_to_many :employees

我的培训事件控制器看起来像:

class TrainingEventsController < ApplicationController
respond_to :json, :html
before_action :filter_time
def index
@events = TrainingEvent.by_month(@time.month).ascending
@events = @events.by_state(params[:state]) if params[:state].present?
@events = @events.by_city(params[:city]) if params[:city].present?
end
def show
@event = TrainingEvent.find_by!(url_name: params[:id])
add_breadcrumb 'Home', root_path
add_breadcrumb 'Training & Events', training_events_path
add_breadcrumb @event.name
end
def region_index
@events = TrainingEvent.by_month(@time.month).ascending
end
private
def filter_time
@time = if params[:month].present?
params[:month].to_datetime
else
Time.zone.now
end
end
end

这里的想法是创建一个单独的方法region_index,然后沿着新的路线传递。因此,在我所在地区的展示页面中,我有以下内容:

<%= link_to 'View Region', region_index_path(@region) %>

链接指向正确的页面,但加载数据是我的难点。以下是视图(在Slim中(:

= content_for :body do
.container.banner-container
.row.mt-4
.col-sm-12
= link_to 'Training Policies', '/training_policies', class: 'custom-button float-right'
= form_tag :training_events, method: :get, id: 'filter-form' do
.row.event-filter.align-items-center
.col-lg-7.col-md-5
= hidden_field_tag :month, params[:month]
.month-selector
= fa_icon 'chevron-left', data: { month: @time.last_month }
h3 #{@time.strftime('%B %Y')}
= fa_icon 'chevron-right', data: { month: @time.next_month }
.col-lg-5.col-md-3.filter-select
=link_to 'View All Trainings', training_events_path, class: 'custom-button float-right'
- (@time.beginning_of_month.to_date..@time.end_of_month.to_date).each do |date|
- events = @events.select { |event| event.time_start.to_date == date }
- if events.count.positive?
.row
.col-sm-12
.event-header
span.date #{date.strftime('%d')}
span #{date.strftime('%A')}
- events.each do |event|
.row.event-row
.col-sm-8
label.event-title #{event.name}
.event-col-details
- if event.time_start.present? && event.time_end.present?
= fa_icon 'clock-o'
p #{event.time_start.strftime('%l:%M %p')} - #{event.time_end.strftime('%l:%M %p')}
- if event.street_address.present?
= fa_icon 'map-marker'
p #{event.street_address}
- if event.city.present? && event.state.present?
= fa_icon 'map-marker'
p #{event.city}, #{event.state}
.col-sm-4.details-cont
= link_to 'view details', training_event_path(event.url_name)
.row
.col-sm-12
.border

我知道我需要点击@events.select,可能会点击地点然后点击地区,但我的头撞到了墙上。我尝试了各种方法来执行@events.locations.select,它会得到一个NoMethodError,甚至是@events.regions.select,它也会得到相同的结果。

那么,我该如何建立贯穿关系并只显示这些结果呢?

编辑:我还尝试在我的TrainingEvents中加入一个范围,认为我可能能够查询出与区域匹配的位置id。location_id如下:

scope :region_index, -> {where(location_id: region.location_id)}

我最终使用

的未定义局部变量或方法"区域">

你是说?关系

区域没有在您的作用域上下文中定义。我看到你已经熟悉了使用变量的范围,看起来你只是忘记了那个细节,需要新的眼光。

scope :region_index, -> (region) { where(location_id: region.location_id)}

最新更新