2017-06-21 4 views
1

Après avec José's upserts post j'ai plusieurs à plusieurs entre deux schémas - Technologie et l'emploi comme ceci:Révisions pour beaucoup à de nombreuses relations

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 

Je continue de recevoir des erreurs de validation lors de la création d'un changeset pour les travaux ...

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]) 

va me remettre un changeset invalide:

#Ecto.Changeset<action: nil, changes: %{}, 
errors: [technologies: {"is invalid", [type: {:array, :map}]}], 
data: #JobJawn.Listing.Job<>, valid?: false> 

Je ne sais pas pourquoi.

Cette pension est publique si elle aide: https://github.com/mcelaney/job_jawn/commit/f408505d165ae71947858676922cf067fe1cc413

+1

Je pense que l'appel à 'put_assoc' doit être:' |> put_assoc (technologies, [tech]) ' –

+0

Ugh - Je suis un idiot ... – Mac

+1

put_assoc a une syntaxe différente de cast_assoc - pouvez-vous reformuler comme réponse pour que je puisse l'accepter? – Mac

Répondre

0

Construire le changeset devrait passer la liste des [tech] comme le dernier argument de 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