跟着José's upserts post我在两个模式之间有多对多的关系 - 技术和工作就像这样:
defmodule JobJawn.Listing.Job do
@moduledoc """
A job is a position for which a JobJawn user might want to apply. This is the
central model for the system - meaning it's the reason a user might want to
visit the system.
"""
use Ecto.Schema
alias JobJawn.Directory.Company
alias JobJawn.Listing.{
Discipline,
EmploymentType,
RoleType,
Technology,
Title}
schema "listing_jobs" do
field :date_missing, :naive_datetime
field :name, :string
field :url, :string
belongs_to :company, Company
belongs_to :discipline, Discipline
belongs_to :employment_type, EmploymentType
belongs_to :role_type, RoleType
belongs_to :title, Title
many_to_many :technologies,
Technology,
join_through: "listing_jobs_technologies"
timestamps()
end
end
defmodule JobJawn.Listing.Technology do
@moduledoc """
Technology serves as a tag to help JobJawn users interested in a certain
technlogy (Elixir, Erlang, Python, C#, F#, etc) locate relevant positions
"""
use Ecto.Schema
schema "listing_technologies" do
field :name, :string
field :job_count, :integer, virtual: true
many_to_many :jobs, JobJawn.Listing.Job, join_through: "listing_jobs_technologies"
timestamps()
end
end
在为作业创建变更集时,我不断收到验证错误...
tech = JobJawn.Listing.Technology |> limit(1) |> JobJawn.Repo.one
JobJawn.Listing.Job
|> limit(1)
|> preload(:technologies)
|> JobJawn.Repo.one
|> change
|> put_assoc(:technologies, with: [tech])
会给我一个无效的变更集:
#Ecto.Changeset<action: nil, changes: %{},
errors: [technologies: {"is invalid", [type: {:array, :map}]}],
data: #JobJawn.Listing.Job<>, valid?: false>
我不知道为什么。
如果它有帮助,这个回购是公开的:https://github.com/mcelaney/job_jawn/commit/f408505d165ae71947858676922cf067fe1cc413
答案 0 :(得分:0)
构建变更集应将[tech]
列表作为最后一个参数传递给put_assoc
tech = JobJawn.Listing.Technology |> limit(1) |> JobJawn.Repo.one
JobJawn.Listing.Job
|> limit(1)
|> preload(:technologies)
|> JobJawn.Repo.one
|> change
|> put_assoc(:technologies, [tech]) # <-- with: keyword removed