У меня есть таблицы user_profile и ролей. В таблице ролей ссылок user_profile есть выход из столбца внешнего ключа role_id.Мне нужно создать build_association между обеими таблицами, прежде чем вставлять данные в таблицу user_profile.
Я пытаюсь сделать это с помощью Ecto.Multi.merge, но есть некоторые ошибки.Это не работает. И я не знаю, где я совершаю ошибку.По моему мнению, функция Ecto.Multi.merge принимает мультиобъект, возвращаемый функцией.Функция передачи напрямую не работает.
def create_user_profile(profile_info, dealer_id) do
import Sage
client_id = Map.get(profile_info, "client_id")
type = Map.get(profile_info, "user_type")
role_name = Map.get(profile_info, "role_name")
role_struct = Repo.get_by(Roles, role_name: role_name)
Ecto.Multi.merge(Repo.get_by(Roles, role_name: role_name), fn %{role: role}->
user_role = Ecto.build_assoc(role, :user_profile)|> UserProfile.changeset(profile_info)
Ecto.Multi.new()
|> Ecto.Multi.insert(:user_role, user_role)end)
|> Ecto.Multi.merge(fn %{user: user} ->
bank_detail =
Ecto.build_assoc(user, :bank_details)
|> BankDetails.changeset(profile_info)
Ecto.Multi.new()
|> Ecto.Multi.insert(:bank_detail, bank_detail)
end)
Вот фрагмент кода.Помогите мне, пожалуйста.Я новичок в этом языке
всякий раз, когда я пытаюсь запустить его с помощью почтальона ... эта ошибка отображается
(FunctionClauseError) no function clause matching in Ecto.Multi.merge/2
(ecto) lib/ecto/multi.ex:228: Ecto.Multi.merge(%Accreditor.Role.Roles{__meta__: #Ecto.Schema.Metadata<:loaded, "roles">, id: 1, inserted_at: ~N[2019-05-31 07:21:21], permissions: #Ecto.Association.NotLoaded<association :permissions is not loaded>, role_name: "SUPER", updated_at: ~N[2019-05-31 07:21:21], user_profile: #Ecto.Association.NotLoaded<association :user_profile is not loaded>, user_type: #Ecto.Association.NotLoaded<association :user_type is not loaded>, user_type_id: 1}, #Function<3.131196970/1 in Accreditor.Accounts.create_user_profile/2>)
(accreditor) lib/core/accounts/accounts.ex:224: Accreditor.Accounts.create_user_profile/2
(accreditor) lib/core/accounts/accounts.ex:195: Accreditor.Accounts.create_account/2
(accreditor) lib/web/controllers/user_controller.ex:17: AccreditorWeb.UserController.create_user/2
(accreditor) lib/web/controllers/user_controller.ex:1: AccreditorWeb.UserController.action/2
(accreditor) lib/web/controllers/user_controller.ex:1: AccreditorWeb.UserController.phoenix_controller_pipeline/2
(primus_web) lib/primus_web/endpoint.ex:1: PrimusWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
(phoenix) lib/phoenix/router/route.ex:39: Phoenix.Router.Route.call/2
(phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
(primus_web) lib/primus_web/endpoint.ex:1: PrimusWeb.Endpoint.plug_builder_call/2
(primus_web) lib/plug/debugger.ex:122: PrimusWeb.Endpoint."call (overridable 3)"/2
(primus_web) lib/primus_web/endpoint.ex:1: PrimusWeb.Endpoint.call/2
(phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2