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