我想从凤凰城的ecto协会生成
JSON.
这是我的协会:
defmodule Blog.Post do use Ecto.Model schema "posts" do field :title,:string field :body,:string has_many :comments,Blog.Comment end end
和:
defmodule Blog.Comment do use Ecto.Model schema "comments" do field :content,:string belongs_to :post,Blog.Post end end
当我生成没有关联的json时,结果如下:
[%Blog.Post{body: "this is the very first post ever!",id: 1,title: "first post"},%Blog.Post{body: "Hello nimrod!!!!",id: 12,title: "hi nimrod"},%Blog.Post{body: "editing the body!!!!",id: 6,title: "hello(edit)"}]
而json看起来像这样
{"posts": [ { "title": "first post","id": 1,"body": "this is the very first post ever!" },{ "title": "hi nimrod","id": 12,"body": "Hello nimrod!!!!" },{ "title": "hello(edit)","id": 6,"body": "editing the body!!!!" } ]}
但结果就是结果
[%Blog.Post{body: "this is the very first post ever!",comments: {Ecto.Associations.HasMany.Proxy,#Ecto.Associations.HasMany<[name: :comments,target: Blog.Post,associated: Blog.Comment,references: :id,foreign_key: :post_id]>},title: "hello(edit)"}]
使用上面的输出我无法创建适当的json输出.我想让json看起来像这样
{"posts": [ { "title": "the title","body": "the body","comments": [{"content": "a comment"},{"content": "another comment"}] } ... ]}
任何帮助,将不胜感激.
解决方法
原文地址:https://www.jb51.cc/js/158741.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。