路過的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
外生結構定義的參數。
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
作爲參數嗎?
只有測試失敗? –
測試通過,但所述控制器操作失敗 – DogEatDog
檢查這一行:'變更= MyRewards.create_deposit_changeset(%{ 「USER_ID」=> customer.id, 「MERCHANT_ID」=> merchant.id, 「量」=>量})' ...從你的場景中沒有函數子句匹配,它是正確的嗎? –