1
与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
我想调用'put_assoc'应该是:' |> put_assoc(技术,[技术])' –
呃 - 我是个白痴...... – Mac
put_assoc有一个不同于cast_assoc的语法 - 你可以改为一个答案,所以我可以接受它吗? – Mac