【发布时间】:2012-10-29 16:00:18
【问题描述】:
这个问题与 AMS 0.8 相关
我有两个模型:
class Subject < ActiveRecord::Base
has_many :user_combinations
has_ancestry
end
class UserCombination < ActiveRecord::Base
belongs_to :stage
belongs_to :subject
belongs_to :user
end
还有两个序列化器:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id
belongs_to :stage
belongs_to :subject
end
class SubjectSerializer < ActiveModel::Serializer
attributes :id, :name, :description, :subjects
def include_subjects?
object.is_root?
end
def subjects
object.subtree
end
end
当UserCombination被序列化时,我想嵌入整个主题子树。
当我尝试使用此设置时,我收到此错误:
undefined method `belongs_to' for UserCombinationSerializer:Class
我尝试将UserCombinationSerializer 更改为:
class UserCombinationSerializer < ActiveModel::Serializer
attributes :id, :subject, :stage
end
在这种情况下,我没有收到任何错误,但 subject 以错误的方式序列化 - 未使用 SubjectSerializer。
我的问题:
- 难道我不能在序列化程序中使用belongs_to 关系吗?
- 如果不是 - 我怎样才能获得想要的行为 - 使用 SubjectSerializer 嵌入主题树?
【问题讨论】:
标签: ruby-on-rails active-model-serializers