在控制器中查询的用法

时间:2016-12-28 10:15:35

标签: ruby-on-rails

我想展示特定赛季和锦标赛的赛事,但我有冠军赛的所有赛事。哪里我不对?

  resources :championships do 
    resources :seasons do 
      resources :events 
    end
  end

class EventsController < ApplicationController
  def index
    @events = @championship.events.where(params[:season_id] == @season.id)
  end
end

Started GET "/championships/2/seasons/2/events" for 127.0.0.1 at 2016-12-28 12:07:07 +0200
Processing by EventsController#index as HTML
  Parameters: {"championship_id"=>"2", "season_id"=>"2"}
  Championship Load (0.3ms)  SELECT  "championships".* FROM "championships" WHERE "championships"."id" = $1 LIMIT 1  [["id", 2]]
  Season Load (0.3ms)  SELECT  "seasons".* FROM "seasons" WHERE "seasons"."id" = $1 LIMIT 1  [["id", 2]]

1 个答案:

答案 0 :(得分:1)

这是您的错误:

@events = @championship.events.where(params[:season_id] == @season.id)

wheretrue

false

但你需要这样做:

@events = @championship.events.where(:season_id => params[:season_id])