【发布时间】:2017-06-22 13:40:52
【问题描述】:
与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>
我不知道为什么。
如果有帮助,这个 repo 是公开的:https://github.com/mcelaney/job_jawn/commit/f408505d165ae71947858676922cf067fe1cc413
【问题讨论】:
-
我认为对
put_assoc的调用应该是:|> put_assoc(technologies, [tech]) -
呃 - 我是个白痴...
-
put_assoc 的语法与 cast_assoc 不同 - 您能否将其改写为答案以便我接受?
标签: elixir phoenix-framework ecto