2016-09-17 7 views
1

카테고리 모델에 ancestry gem을 사용하고 있고 arrange_serializable 메소드를 호출 할 때 출력을 cusmized하고 관련 모델의 메소드에서 출력을 추가하고 싶습니다. 대신의Ancestry - arrange_serializable 메소드의 출력 사용자 정의

{ID : 1, 이름 : "XYZ", 어린이 : {ID : 11 이름 : "XYZ", 어린이 ...}} 내가 필요

: {ID : 1 , custom_method : "ABC", 어린이 : {id : 11, custom_method : "ABC", 이름 : "XYZ", 어린이 ...}}

저는 운이 없어도 며칠을 보냈습니다.

팁 어떤 방법을 원하십니까?

감사합니다. Miro

답변

2

사용자 지정 arrange_serializable 메서드를 정의해야합니다. 다음과 같이 할 수 있습니다.

TreeNode.arrange_serializable do |parent, children| 
    { id: parent.id, 
    custom_method: "ABC", 
    children: children } 
end 
0

고유 한 custom_arrange 메소드를 만들 수 있습니다. 이것은 귀하의 예를 위해 작동합니다.

def custom_arrange nodes = nil 
    nodes = Post.arrange if nodes.nil? 
    nodes.map do |parent, children| 
     { 
      id: parent.id, 
      custom_method: "ABC", 
      children: custom_arrange(children) 
     } 
    end 
end