【发布时间】:2017-06-28 22:10:10
【问题描述】:
我在我的 Rails 5 应用程序中使用 active_model_serializers gem。我在/app/seralizers、user_serializer.rb、sector_serializer.rb 和slot_serializer.rb 中创建了一些序列化程序文件。
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 的那一行出现错误,我不知道为什么。
感谢任何帮助。谢谢!
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-5 active-model-serializers