Inputs_for not working for has_many through relationship

I have a question related to has_many through associations.

The Ecto docs list an example like the following.

has_many :comments, Comment
has_many :comments_authors, through: [:comments, :author]

I have a similar relationship and it seems to be working correctly, but I run into a problem when I try to use the :comments_authors relationship with inputs_for. Here’s an example that works for :comments but fails for :comments_authors.

inputs_for(f, :comments, fn p -> hidden_input(p, :id) end)
{:safe, [....listofcomments...]}
inputs_for(f, :comments_authors, fn p -> hidden_input(p, :id) end)
** (ArgumentError) could not generate inputs for :comments_authors from .... Check the field exists and it is one of embeds_one, embeds_many, has_one, has_many, belongs_to or many_to_many
    (phoenix_ecto) lib/phoenix_ecto/html.ex:217: Phoenix.HTML.FormData.Ecto.Changeset.find_inputs_for_type!/2
    (phoenix_ecto) lib/phoenix_ecto/html.ex:35: Phoenix.HTML.FormData.Ecto.Changeset.to_form/4
    (phoenix_html) lib/phoenix_html/form.ex:287: Phoenix.HTML.Form.inputs_for/4

Does anyone know what I’m doing wrong?


How are you creating your changeset?

From the documentation of Ecto.Schema.has_many/3:

(fat mine, italic original emphasis)

In fact, given :through associations are read-only, using the Ecto.assoc/2 format is the preferred mechanism for working with through associations.

I’m not sure if inputs make sense in this case…


I missed that important little bit of doc. Thanks NobbZ.