http://apidock.com/rails/ActiveRecord/Serialization/to_json
konata.to_json(:include => :posts) # => {"id": 1, "name": "Konata Izumi", "age": 16, "created_at": "2006/08/01", "awesome": true, "posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"}, {"id": 2, author_id: 1, "title": "So I was thinking"}]}
使用include包含有one_to_many关系的对象
本文介绍 Rails 框架中 ActiveRecord 对象如何使用 to_json 方法进行序列化,并包含关联对象。通过设置 include 参数可以将 one_to_many 的关联数据一并转换为 JSON 格式。
3226

被折叠的 条评论
为什么被折叠?



