0

私はRails 5アプリでactive_model_serializers gemを使用しています。 /app/seralizersuser_serializer.rbsector_serializer.rb、およびslot_serializer.rbにいくつかのシリアライザファイルを作成しました。Rails NoMethodError:#<ユーザ:0x007f62dbbe62f8>のための未定義メソッド `includes '

class UserSerializer < ActiveModel::Serializer 
    attributes :id, :first_name, :last_name, :email, :phone, :admin, :auth_token, :organization_id 

    has_many :sectors 
    has_many :slots 
    has_many :elements 
end 

class SectorSerializer < ActiveModel::Serializer 
    attributes :id, :user_id, :sector_number, :title 

    belongs_to :user 
    has_many :slots 
    has_many :elements 
end 

class SlotSerializer < ActiveModel::Serializer 
    attributes :id, :user_id, :sector_id, :sector_number, :title, :slot_number 

    belongs_to :user 
    belongs_to :sector 
    has_many :elements 
end 

そして、私のコントローラのコードでは、私が持っている:

class Api::V1::UsersController < API::V1::BaseController 
    respond_to :json 

    def sky 
     @user = User.find_by_id(params[:user_id]).includes(:sectors, :slots) 

     if @user 
     render json: @user 
     else 
      raise "Unable to get Sky" 
     end 
    end 
end 

私のサーバーは、私は.includesを行うと、私はその理由を把握することはできません行でエラーを投げています。

何か助けていただければ幸いです。ありがとう!

答えて

2

変更

@user = User.find_by_id(params[:user_id]).includes(:sectors, :slots) 

@user = User.includes(:sectors, :slots).find_by_id(params[:user_id]) 

へのポイントは、クラス(つまり ActiveRecord::Base/ ApplicationRecordから継承)ではなく、単一のユーザーオブジェクトに includesを呼び出す必要があります。

+0

パーフェクト。本当にありがとうUrsus –

+0

私の喜びSir :) – Ursus

関連する問題