黃金俠

Ruby on Rails / Rubygems / Javascript / Git

巢狀 (nested) to_json / to_xml 應用

| Comments

在做 api 輸出(json,xml等)時, 想要將 model 中的某些 attribute 或關連的其他 model 一起輸出, rails 中的 to_json 或 to_xml 更可以針對 method 做輸出!

參考

  • ApiDock

http://apidock.com/rails/ActiveRecord/Serialization/to_json

  • Stackoverflow

http://stackoverflow.com/questions/4443218/ror-nested-include-to-include-sub-resources-in-to-xml-to-json

說明

  • 參數為 include
  • 可以搭配 only, methods 做部分 attribute 或 method 的輸出

範例

1
2
3
4
5
6
foo.to_json(:include =>
               { :emails => { :only => :address },
               :friends => { :only => :name,
                 :include => { :emails => { :only => :address } }
               }
             })

Comments