     end)
   end
 
+  defp update_question_and_tags(changeset) do
+    Repo.transaction(fn ->
+      case (Repo.update(changeset) |> remove_tags(changeset) |> insert_tags(changeset)) do
+        {:error, changeset} ->
+          Repo.rollback(changeset)
+        {:ok, question, tags} ->
+          Map.merge(question, %{tags: tags})
+      end
+    end)
+  end
+
   defp insert_tags({:error, changeset}, _), do: {:error, changeset}
-  defp insert_tags({:ok, question}, %{changes: changes}) do
-    tag_id_to_changeset = fn tag ->
-      QuestionTag.changeset(Map.merge(tag, %{"question_id" => question.id}))
+  defp insert_tags({:ok, question}, changeset) do
+    tag_id_to_changeset = fn answer ->
+      QuestionTag.changeset(%QuestionTag{}, %{"tag_id" => answer["tag"],
+                                              "required" => answer["required"],
+                                              "question_id" => question.id})
     end
 
     insert_changeset = fn

        end
    end

    Enum.map(changes.tags, tag_id_to_changeset)
    |> Enum.reduce({:ok, question, []}, insert_changeset)
  end
end


  def show(conn, _) do
    question = conn.assigns[:question]
    answer = Repo.all(from(qt in QuestionTag, where: qt.question_id == ^question.id))
    question = Map.merge(question, %{answer: answer})

    render(conn, "show.json", %{question: question})
  end

  def update(conn, question_params) do
