sla*_*ili 7 associations elixir ecto
假设我有这些模式:
defmodule Sample.Post do
use Ecto.Schema
schema "post" do
field :title
has_many :comments, Sample.Comment
end
end
defmodule Sample.User do
use Ecto.Schema
schema "user" do
field :name
has_many :comments, Sample.Comment
end
end
defmodule Sample.Comment do
use Ecto.Schema
schema "comment" do
field :text
belongs_to :post, Sample.Post
belongs_to :user, Sample.User
end
end
Run Code Online (Sandbox Code Playgroud)
我的问题是如何Ecto.build_assoc用来保存评论?
iex> post = Repo.get(Post, 13)
%Post{id: 13, title: "Foo"}
iex> comment = Ecto.build_assoc(post, :comments)
%Comment{id: nil, post_id: 13, user_id: nil}
Run Code Online (Sandbox Code Playgroud)
到目前为止还可以,我需要做的就是使用相同的函数来设置user_id我的Comment结构,但是由于返回值build_assoc是Commentstruct,我不能使用相同的函数
iex> user = Repo.get(User, 1)
%User{id: 1, name: "Bar"}
iex> Ecto.build_assoc(user, :comment, comment)
** (UndefinedFunctionError) undefined function: Sample.Comment.delete/2
...
Run Code Online (Sandbox Code Playgroud)
我有两个选择,但它们对我来说都不好看:
首先是user_id手动设置!
iex> comment = %{comment| user_id: user.id}
%Comment{id: nil, post_id: 13, user_id: 1}
Run Code Online (Sandbox Code Playgroud)
第二个是将结构转换为map ...我甚至不想去那里
有什么建议吗?
你为什么不想转换struct来映射?这真的很容易.
build_assoc期望将属性映射作为最后一个值.在内部,它试图删除键:__meta__.结构具有编译时保证,它们将包含所有已定义的字段,因此您将获得:
** (UndefinedFunctionError) undefined function: Sample.Comment.delete/2
Run Code Online (Sandbox Code Playgroud)
但你可以写:
comment = Ecto.build_assoc(user, :comment, Map.from_struct comment)
Run Code Online (Sandbox Code Playgroud)
一切都会好起来的.