【发布时间】:2017-02-22 04:05:32
【问题描述】:
在传递 1 到多关联的父记录的 ID 值时,我在创建子记录时收到关联错误。
错误说我需要预加载User 这是Transaction 的父记录,
1) test creates and renders resource when data is valid (MyRewardsWeb.TransactionControllerTest)
test/controllers/transaction_controller_test.exs:36
** (RuntimeError) attempting to cast or change association `user` from `MyRewards.Transaction` that was not loaded. Please preload your associations before manipulating them thr
ough changesets
stacktrace:
(ecto) lib/ecto/changeset/relation.ex:66: Ecto.Changeset.Relation.load!/2
(ecto) lib/ecto/repo/schema.ex:514: anonymous fn/4 in Ecto.Repo.Schema.surface_changes/4
(elixir) lib/enum.ex:1623: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/repo/schema.ex:503: Ecto.Repo.Schema.surface_changes/4
(ecto) lib/ecto/repo/schema.ex:186: Ecto.Repo.Schema.do_insert/4
(my_rewards_web) web/controllers/transaction_controller.ex:20: MyRewardsWeb.TransactionController.create/2
(my_rewards_web) web/controllers/transaction_controller.ex:1: MyRewardsWeb.TransactionController.action/2
(my_rewards_web) web/controllers/transaction_controller.ex:1: MyRewardsWeb.TransactionController.phoenix_controller_pipeline/2
(my_rewards_web) lib/my_rewards_web/endpoint.ex:1: MyRewardsWeb.Endpoint.instrument/4
(my_rewards_web) lib/phoenix/router.ex:261: MyRewardsWeb.Router.dispatch/2
(my_rewards_web) web/router.ex:1: MyRewardsWeb.Router.do_call/2
(my_rewards_web) lib/my_rewards_web/endpoint.ex:1: MyRewardsWeb.Endpoint.phoenix_pipeline/1
(my_rewards_web) lib/my_rewards_web/endpoint.ex:1: MyRewardsWeb.Endpoint.call/2
(phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
test/controllers/transaction_controller_test.exs:41: (test)
在我的控制器方法中,我发现 User 和 Merchant 记录都是 Transaction 结构的父记录,并在创建变更集时将各自的记录 ID 传递给
def create(conn, %{"merchant_id" => merchant_id, "mobile" => mobile, "amount" => amount}) do
customer = MyRewards.find_user!(%{"mobile" => mobile})
merchant = Repo.get(MyRewards.Merchant, merchant_id)
changeset = MyRewards.create_deposit_changeset(%{"user_id" => customer.id, "merchant_id" => merchant.id, "amount" => amount })
case Repo.insert(changeset) do
{:ok, transaction} ->
conn
|> put_status(:created)
|> put_resp_header("location", transaction_path(conn, :show, transaction))
|> render("show.json", transaction: transaction)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(MyRewardsWeb.ChangesetView, "error.json", changeset: changeset)
end
end
这个函数只是传递信息并返回变更集:
def create_deposit_changeset(user_id: user_id, merchant_id: merchant_id, amount: amount) do
MyRewards.Transaction.deposit(%MyRewards.Transaction{}, %{ merchant_id: merchant_id, user_id: user_id, amount: Money.new(amount) })
end
在结构中,我定义了关系并将user_id 和merchant_id 正确转换为Transaction Ecto 结构定义中的参数。
defmodule MyRewards.Transaction do
@entry_types [:credit, :debit]
use MyRewards.Model
schema "my_rewards_transactions" do
field :amount, Money.Ecto.Type
field :type, :string
belongs_to :user, MyRewards.User
belongs_to :merchant, MyRewards.Merchant
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:amount, :type, :user_id, :merchant_id])
|> validate_required([:amount, :type])
end
def deposit(struct, params) do
struct
|> cast( %{type: "debit"}, [:type])
|> changeset(params)
end
def widthdraw(struct, params) do
struct
|> cast( %{type: "credit"}, [:type])
|> changeset(params)
end
end
当我在我的测试用例中测试函数时,它可以正常进入事务。
MyRewards.create_deposit_changeset(user_id: customer.id, merchant_id: merchant.id, amount: x)
但是当我在 Phoenix 控制器中运行它时,它失败了。我不确定为什么它会失败或要求预加载父关联。我应该在某处做一个put_assoc,而不是直接将user_id 和merchant_id 转换为参数吗?
【问题讨论】:
-
只有测试失败?
-
测试通过但控制器操作失败
-
检查这一行:
changeset = MyRewards.create_deposit_changeset(%{"user_id" => customer.id, "merchant_id" => merchant.id, "amount" => amount })...从你的场景中没有匹配的函数子句,对吗? -
这不是问题,但谢谢。我重载了函数调用以接受两种类型的参数
标签: elixir phoenix-framework ecto