【问题标题】:Rails display results from a through relationshipRails 显示直通关系的结果
【发布时间】:2018-11-15 03:05:16
【问题描述】:

所以现在我正在显示一个 TrainingEvents 索引页面,其中包含每个位置的所有事件。我正在尝试为该特定区域创建一个 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

我的 TrainingEventsController 看起来像:

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 中加入一个范围,认为我可以使用以下内容查询与 region.location_id 匹配的位置 ID:

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

我结束了

未定义的局部变量或方法“区域”

你的意思是?关系

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

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

    【讨论】:

    • 语法错误,意外'|' :region_index, -> (区域) { |区域| where(location_id: reg ^。还没有深入研究这是怎么回事。哦,我在@events.region_index.select 下应用了region_index
    • 是的,我忘了 || 没有在 lambdas 中使用
    • 更多细节在这里:guides.rubyonrails.org/…
    • 参数数量错误(给定 0,预期为 1)。我认为这意味着我需要在 @events.region_index.select 上传递一些东西,比如 @events.region_index(@region.id).select 但它不喜欢这样
    • @region.id 我猜是一个整数。我很确定它不会回复location_id。您认为必须将某些内容传递给region_index 是对的。你试过@events.region_index(@region).select没有id吗? select 也需要参数。你提供了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2012-05-02
    • 1970-01-01
    • 2013-06-23
    相关资源
    最近更新 更多