我有league
模型和league
has_many Teams
。我只希望用户能够为每个联盟创建一个团队。我发现这个验证很棘手。这是我目前正在尝试的。
def changeset(%Team{} = team, attrs \\ %{}) do
team
|> cast(attrs, [:name, :league_id, :user_id])
|> validate_required([:name, :league_id, :user_id])
|> one_team_per_user_for_leagues
end
defp one_team_per_user_for_leagues(team) do
if team.changes == %{} do
team
else
league = Repo.get!(League, team.changes[:league_id]) |> Repo.preload(:teams)
Enum.map(league.teams, fn(team) -> team = team end) |> Enum.any?
end
end
但是我收到了这个错误:no function clause matching in Ecto.Repo.Schema.insert/4
Request: POST /teams
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Ecto.Repo.Schema.insert/4
(ecto) lib/ecto/repo/schema.ex:157: Ecto.Repo.Schema.insert(Statcasters.Repo, Ecto.Adapters.Postgres, false, [])
(statcasters) lib/statcasters_web/controllers/team_controller.ex:21: StatcastersWeb.TeamController.create/2
(statcasters) lib/statcasters_web/controllers/team_controller.ex:1: StatcastersWeb.TeamController.action/2
(statcasters) lib/statcasters_web/controllers/team_controller.ex:1: StatcastersWeb.TeamController.phoenix_controller_pipeline/2
(statcasters) lib/statcasters_web/endpoint.ex:1: StatcastersWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
(statcasters) lib/statcasters_web/endpoint.ex:1: StatcastersWeb.Endpoint.plug_builder_call/2
(statcasters) lib/plug/debugger.ex:99: StatcastersWeb.Endpoint."call (overridable 3)"/2
(statcasters) lib/statcasters_web/endpoint.ex:1: StatcastersWeb.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/cameronbass/Desktop/Play/statcasters/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
这也感觉不像“Elixir Way”任何人都可以帮我解开这个问题吗?
答案 0 :(得分:2)
Ecto.Changeset.cast/4
会返回Ecto.Changeset
,以及之后传输的所有功能,例如validate
。您的one_team_per_user_for_leagues/1
也应遵守此规则:
defp one_team_per_user_for_leagues(%Ecto.Changeset{} = changes) do
...
end
另一个小问题是如何通知Ecto
任何错误:要在errors
结构中返回非空Ecto.Changeset
值:
defp one_team_per_user_for_leagues(%Ecto.Changeset{} = changes) do
case get_or_create_teams_in_this_league() do # to implement
{:existing, %Team{}} ->
new_errors = ...
%{changes | errors: new_errors ++ changes.errors, valid?: false}
{:new, %Team{}} ->
changes
|> put_assoc(:team, ...)
end
end