2012-10-29 28 views
23

Câu hỏi này gắn liền với AMS 0.8kích hoạt mẫu serializers belongs_to

Tôi đã có hai mô hình:

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 

Và hai serializers:

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 

Khi một UserCombination là serialized, Tôi muốn nhúng toàn bộ cây con của các đối tượng.

Khi tôi cố gắng sử dụng thiết lập này, tôi nhận được lỗi này:

undefined method `belongs_to' for UserCombinationSerializer:Class 

tôi đã cố gắng thay đổi UserCombinationSerializer này:

class UserCombinationSerializer < ActiveModel::Serializer 
    attributes :id, :subject, :stage 
end 

Trong trường hợp này tôi nhận được không có lỗi, nhưng subject là tuần tự theo cách sai - không sử dụng SubjectSerializer.

Câu hỏi của tôi:

  1. nên tôi không thể sử dụng một mối quan hệ belongs_to trong serializer?
  2. Nếu không - làm thế nào tôi có thể có được hành vi mong muốn - nhúng cây chủ đề bằng cách sử dụng SubjectSerializer?

Trả lời

37

Đây không phải là thực sự thanh lịch nhưng có vẻ như được làm việc:

class UserCombinationSerializer < ActiveModel::Serializer 
    attributes :id, :stage_id, :subject_id 

    has_one :subject 
end 

tôi không thực sự thích gọi has_one trong khi nó thực sự là một hiệp hội belongs_to:/

EDIT: Coi thường nhận xét của tôi về has_one/thuộc về sự mơ hồ, tài liệu thực sự khá rõ ràng về điều đó: http://www.rubydoc.info/github/rails-api/active_model_serializers/frames

+4

Được rồi, yeah, công trình này. Tôi nghĩ rằng tôi hiểu phương pháp 'has_one' tốt hơn bây giờ. Trong một 'Serializer', điều duy nhất thú vị là liệu một phương thức trả về một hay nhiều đối tượng. Vì vậy, phân biệt giữa has_one và thuộc_to không phải là thú vị. Đó là một kinda suboptimal rằng từ ngữ trùng với thuật ngữ ActiveRecord, vì các điều khoản không có nghĩa là cùng một điều. – Jesper

+0

Tôi đã gặp phải sự cố tương tự này gần đây. Có, sử dụng thuộc tính has_one: làm việc cho tôi. –

+11

Các tài liệu cho 'ActiveModel :: Serializer' explictly tiểu bang:" Serializers chỉ quan tâm đến multiplicity, và không sở hữu. Thuộc về ActiveRecord hiệp hội có thể được bao gồm bằng cách sử dụng has_one trong serializer của bạn. " – awendt

3

Điều gì sẽ xảy ra nếu bạn thử với điều gì đó li ke this:

class UserCombinationSerializer < ActiveModel::Serializer 
    attributes :subject, 
      :stage, 
      :id 

    def subject 
    SubjectSerializer.new(object.subject, { root: false }) 
    end 

    def stage 
    StageSerializer.new(object.stage, { root: false }) 
    end 
end 
0

Trong bộ nối tiếp mô hình đang hoạt động 0-10-ổn định, belongs_to hiện khả dụng.

belongs_to :author, serializer: AuthorPreviewSerializer 
belongs_to :author, key: :writer 
belongs_to :post 
belongs_to :blog 
def blog 
    Blog.new(id: 999, name: 'Custom blog') 
end 

https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/serializers.md#belongs_to

Vì vậy, bạn có thể làm:

class UserCombinationSerializer < ActiveModel::Serializer 
    attributes :id 
    belongs_to :stage, serializer: StageSerializer 
    belongs_to :subject, serializer: SubjectSerializer 
end