Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 5,活动的"模型"序列化程序,其中包括订单以及为什么我可以';t在';显示';控制器的方法? 我使用的是Rails 5,gem‘active_model_serializers’,我遇到了这样的情况:_Ruby On Rails_Ruby On Rails 4_Serialization_Ruby On Rails 5_Active Model Serializers - Fatal编程技术网

Ruby on rails Rails 5,活动的"模型"序列化程序,其中包括订单以及为什么我可以';t在';显示';控制器的方法? 我使用的是Rails 5,gem‘active_model_serializers’,我遇到了这样的情况:

Ruby on rails Rails 5,活动的"模型"序列化程序,其中包括订单以及为什么我可以';t在';显示';控制器的方法? 我使用的是Rails 5,gem‘active_model_serializers’,我遇到了这样的情况:,ruby-on-rails,ruby-on-rails-4,serialization,ruby-on-rails-5,active-model-serializers,Ruby On Rails,Ruby On Rails 4,Serialization,Ruby On Rails 5,Active Model Serializers,控制员/联盟\控制员.rb: class LeaguesController < ApplicationController before_action :authenticate_user before_action :set_league, only: [:show, :update, :destroy] # GET /leagues def index @leagues = League.all render json: @leagues end

控制员/联盟\控制员.rb

class LeaguesController < ApplicationController
  before_action :authenticate_user
  before_action :set_league, only: [:show, :update, :destroy]

  # GET /leagues
  def index
    @leagues = League.all
    render json: @leagues
  end

  # GET /leagues/1
  def show
    render json: @league, include: ['teams'] # Here I need some way to order
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_league
      @league = League.find(params[:id])
    end
class League < ApplicationRecord
  has_many :teams
end
class LeagueSerializer < ActiveModel::Serializer

  attributes :id, :name, :address, :teams_num

  has_many :teams
  has_many :menus, serializer: ProductSerializer

end
逻辑是什么?我必须在我的序列化程序中订购?在我的模型中? 为什么我不能用这样的东西?它给了我“未定义的方法包括”。为什么?


@league=@league.includes(:team).order(goals::desc)
不起作用,因为它不是活动记录范围。它是活动记录的一个实例。相反,您可以在首先提取
@league
的位置添加
includes
,例如
@league=league.find(params[:id])。includes(:team)。order(goals::desc)
。。。我想订购也不会用这个。。。当你只有一个联赛/球队时,你的进球顺序是什么?你的意思是说
包含(:团队)
吗?是的,我需要订购包含(:团队)并且我还要问性能包括在哪里。如果你还不能让它实际工作,不要担心性能;)让它工作,然后让它快速工作。好的,怎么做?我可以在哪里订购包含的团队?你试过我的建议了吗?:)<代码>@league=@league.includes(:team).order(goals::desc)不起作用,因为它不是活动记录范围。它是活动记录的一个实例。相反,您可以在首先提取
@league
的位置添加
includes
,例如
@league=league.find(params[:id])。includes(:team)。order(goals::desc)
。。。我想订购也不会用这个。。。当你只有一个联赛/球队时,你的进球顺序是什么?你的意思是说
包含(:团队)
吗?是的,我需要订购包含(:团队)并且我还要问性能包括在哪里。如果你还不能让它实际工作,不要担心性能;)让它工作,然后让它快速工作。好的,怎么做?我可以在哪里订购包含的团队?你试过我的建议了吗?:)
def show
  render json: @league, include: ['teams'], :order => 'goals DESC'
end
def show
  @league = @league.includes(:team).order(goals: :desc)
  render json: @league
end