Casting through both nested (cast_assoc) and non-nested forms

Using Phoenix 1.3 and Contexts

I have task_lists and task_items tables. What I want is to cast data both through nested (in ListController/new) and non-nested (ItemController/new) forms.

The problem seems to be lying in Task.Item.changeset

  def changeset(%Task.Item{} = item, attrs) do
    |> cast(attrs, [:text, :list_id])
    |> validate_required([:text, :list_id])

This way I can cast data through Task.ItemController/new with no problem. the foreign key is there to be saved.

However, I cannot cast data through Task.ListController/new unless I remove :list_id for cast_assoc in Task.Item.changeset already sets it up

Ok. I managed to solve it.

I created another changeset in Task/List. (Task.List.items_changeset)

def items_changeset(%Task.Item{} = item, attrs) do
    |> cast(attrs, [:text])
    |> validate_required([:text])