gpt4 book ai didi

elixir - 一次保存多个 has_many 关联 Phoenix 1.3

转载 作者:行者123 更新时间:2023-12-02 02:57:03 25 4
gpt4 key购买 nike

我有两个模型:

 defmodule TransactionApi.Messages.Event do
use Ecto.Schema
import Ecto.Changeset
alias TransactionApi.Messages.Event
alias TransactionApi.Messages.EventDetail

schema "events" do
field :city, :string
field :email, :string
field :ip, :string
field :sender, :string
field :status, :string
field :subject, :string
field :template, :string
field :ts, :utc_datetime
field :uniq_id, :string
field :user_agent, :string

has_many :event_details, EventDetail

timestamps()
end

@doc false
def changeset(%Event{} = event, attrs) do
event
|> cast(attrs, [:sender, :uniq_id, :ts, :template, :subject, :email, :status, :ip, :city, :user_agent])
|> cast_assoc(:event_details)
|> validate_required([:sender, :uniq_id, :ts, :subject, :email, :status])
end
end

defmodule TransactionApi.Messages.EventDetail do
use Ecto.Schema
import Ecto.Changeset
alias TransactionApi.Messages.EventDetail
alias TransactionApi.Messages.Event


schema "event_details" do
field :ts, :utc_datetime
field :url, :string

belongs_to :event, Event, foreign_key: :event_id
timestamps()
end

@doc false
def changeset(%EventDetail{} = event_detail, attrs) do
event_detail
|> cast(attrs, [:url, :ts, :event_id])
|> validate_required([:ts, :event_id])
end
end

我想在我的 event Controller 中保存事件及其关联的 EventDetail:

  def create(conn, %{"mandrill_events" => event_params}) do
params = parse_incoming event_params
with {:ok, %Event{} = event} <- Messages.create_event(params) do
conn
|> put_status(:created)
|> put_resp_header("location", event_path(conn, :show, event))
|> render("show.json", event: event)
end
end

这是我构建的 params map 的样子:

%{      
city: "Oklahoma City",
email: "example.webhook@mandrillapp.com",
event: "open",
event_details: [
%{"ts" => #DateTime<2013-04-04 21:31:51Z>, "url" => "http://mandrill.com"},
%{"ts" => #DateTime<2013-04-04 21:31:51Z>}
],
ip: "127.0.0.1",
sender: "example.sender@mandrillapp.com",
status: "sent",
subject: "This an example webhook message",
tags: ["webhook-example"],
template: nil,
ts: #DateTime<2018-02-12 12:33:48Z>,
uniq_id: "exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
user_agent: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.8) Gecko/20100317 Postbox/1.1.3"
}

但我的 api 返回错误 : {"errors":{"event_details":[{"event_id":["can't be blank"]},{"event_id":["can't留空"]}]}}

我如何确保关联的 EventDetail 正确地保留了对 Event 表的 foreign_key 引用,这里的“最佳实践”方法是什么?

编辑:

在 Phoenix 1.3 中,他们似乎添加了位于模型上下文中的 create_[table_name] 并处理 changeset 和插入(我认为更改想要将 web 相关部分与应用程序分开,但不确定):

  def create_event(attrs \\ %{}) do
%Event{}
|> Event.changeset(attrs)
|> Repo.insert()
end

最佳答案

我想出了一种方法来保存包含嵌套关联的 map ,但由于我是 Phoenix 和 Elixir(一般的函数式编程)的新手,我不确定这是正确/最佳实践方法。

%{      
city: "Oklahoma City",
email: "example.webhook@mandrillapp.com",
event: "open",
event_details: [
%{"ts" => #DateTime<2013-04-04 21:31:51Z>, "url" => "http://mandrill.com"},
%{"ts" => #DateTime<2013-04-04 21:31:51Z>}
],
ip: "127.0.0.1",
sender: "example.sender@mandrillapp.com",
status: "sent",
subject: "This an example webhook message",
tags: ["webhook-example"],
template: nil,
ts: #DateTime<2018-02-12 12:33:48Z>,
uniq_id: "exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
user_agent: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.8) Gecko/20100317 Postbox/1.1.3"
}

event_controller.ex

  def create(conn, %{"mandrill_events" => event_params}) do
params = parse_incoming event_params
with {:ok, %Event{} = event} <- Messages.create_event(params) do
event
|> Messages.add_event_details(params[:event_details])

conn
|> put_status(:created)
|> put_resp_header("location", event_path(conn, :show, event))
|> render("show.json", event: event)
end
end

然后在我的 Message 上下文中:

  # persist an `event`

def create_event(%{event: event_params} \\ %{}) do
%Event{}
|> Event.changeset(event_params)
|> Repo.insert()
end

# persist a collection of `event_details`

def add_event_details(%Event{} = event, details) do
Enum.map(details, fn(event_detail) ->
event_detail
|> Map.put("event_id", event.id)
|> create_event_detail
end)
end

关于elixir - 一次保存多个 has_many 关联 Phoenix 1.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48746674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com