Clerisse Lucas
Clerisse Lucas

Reputation: 43

Some problems with associations in ecto

I've started recently to work with phoenix, the fact is i'm usually working with NO SQL databases.

I'm working on a classic blog to integrate associations. I've created a basic association :

Here are those schemas :

schema "categories" do
    field :category_name, :string
    field :logo, :string
    has_many :posts, Lclp.Post

    timestamps()
  end

schema "posts" do
 field :author, :string
 field :content, :string
 field :content_raw, :string
 field :desc, :string
 field :logo, :string
 field :title, :string
 belongs_to :category, Lclp.Category

 timestamps()
end

This one, working fine, i can call the category name from the category_id of a post after a preload.

The problem is when inserting a post, i've created a form for create a post where i can select the category, set the title, etc... My controller get all the data by pattern matching :

def create_post(conn, %{"post" => post}) do
      IO.inspect(post)
      changeset = Lclp.Post.add_post(%Lclp.Post{}, post)
      IO.inspect(changeset)

      case Lclp.Repo.insert(changeset) do
        {:ok, data} ->
          conn
          |> put_flash(:info, "Post bien ajouté")
          |> redirect(to: adminpost_path(conn, :index))
        {:error, changeset} ->
          conn
          |> assign(:section, "Creation d'un post (callback error)")
          |> render("new.html", changeset: changeset, categories_tab: Lclp.Category.get_categories_name)
      end
    end

All fields are added to the DB except the category_id and which stay at NULL.

The only solution i found out to insert a category_id is that one :

def changeset(post, attrs \\ %{}) do
    post
    |> cast(attrs, [:title, :author, :desc, :content, :content_raw, :logo])
    |> validate_required([:title, :author, :desc, :content, :content_raw])
  end

  def add_post(post, attrs) do
    post
    |> changeset(attrs)
    |> change(%{category_id: String.to_integer(Map.fetch!(attrs, "category_id"))})
  end

Which work well, but i have the feeling its not the good way to do it.

If someone have a an idea of what i'm doing wrong.

Thanks

Upvotes: 1

Views: 68

Answers (1)

Dogbert
Dogbert

Reputation: 222040

You don't need to manually do this, just add category_id to the list of allowed fields passed to cast:

|> cast(attrs, [:title, :author, :desc, :content, :content_raw, :logo, :category_id])
                                                                       ^^^^^^^^^^^^

Upvotes: 1

Related Questions