【发布时间】:2015-09-11 16:49:40
【问题描述】:
知道如何通过关联在 has_many 中实现 active_mode_serializer。
我有三个模型:
class Menu < ActiveRecord::Base
has_many :menu_details, inverse_of: :menu, dependent: :destroy
has_many :foods, through: :menu_details
end
class Food < ActiveRecord::Base
has_many :menu_details
has_many :menu, through: :menu_details
ends
class MenuDetail < ActiveRecord::Base
belongs_to :menu
belongs_to :food
end
活动模型序列化器
class MenuSerializer < ActiveModel::Serializer
attributes :id, :name, :price
has_many :menu_details
has_many :foods
end
class MenuDetailSerializer < ActiveModel::Serializer
attributes :id
has_one :menu
has_one :food
end
class FoodSerializer < ActiveModel::Serializer
attributes :id
has_many :menu_details
has_many :menu
end
我总是收到此错误:
Completed 500 Internal Server Error in 24ms
SystemStackError (stack level too deep):
app/controllers/menus_controller.rb:7:in `all'
【问题讨论】:
-
我的序列化器结构是否正确?这就是为什么我得到那个错误。我不知道如何在
has_many through中使用它。
标签: ruby-on-rails ruby activerecord serialization