我想从凤凰城的一个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
Run Code Online (Sandbox Code Playgroud)
和:
defmodule Blog.Comment do
use Ecto.Model
schema "comments" do
field :content, :string
belongs_to :post, Blog.Post
end
end
Run Code Online (Sandbox Code Playgroud)
当我生成没有关联的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)"}]
Run Code Online (Sandbox Code Playgroud)
而json看起来像这样
{"posts": [
{
"title": "first post",
"id": 1,
"body": "this is the …
Run Code Online (Sandbox Code Playgroud)