defmodule Discuss.Comment do
use Discuss.Web, :model
@derive {Poison.Encoder, only: [:content, :user]}
schema "comments" do
field :content, :string
belongs_to :user, Discuss.User
belongs_to :topic, Discuss.Topic
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:content])
|> validate_required([:content])
end
end
defmodule Discuss.Topic do
use Discuss.Web, :model
schema "topics" do
field :title, :string
belongs_to :user, Discuss.User
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title])
|> validate_required([:title])
end
end
This is saying that you have a query where you are trying to load (or otherwise use) the comments filed, but Discus.Topic has no such association. Looking at your schema, Discuss.Topic has an association to Discuss.User, but not to comments.
You will need to add that in the schema for Discuss.Topic, something like: has_many: :comments, Discuss.Comments (assuming that is the name of it)
one solution was:
And your topic does not define has many comments.
The problem was not the version, it helped me a lot. Thanks!
defmodule Discuss.Comment do
use Discuss.Web, :model
@derive {Poison.Encoder, only: [:content, :user]}
schema "comments" do
field :content, :string
belongs_to :user, Discuss.User
belongs_to :topic, Discuss.Topic
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:content])
|> validate_required([:content])
end
end
defmodule Discuss.Topic do
use Discuss.Web, :model
schema "topics" do
field :title, :string
belongs_to :user, Discuss.User
has_many :comments, Discuss.Comment
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title])
|> validate_required([:title])
end
end
defmodule Discuss.User do
use Discuss.Web, :model
@derive {Poison.Encoder, only: [:email]}
schema "users" do
field :email, :string
field :provider, :string
field :token, :string
has_many :topics, Discuss.Topic
has_many :comments, Discuss.Comment
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:email, :provider, :token])
|> validate_required([:email, :provider, :token])
end
end
defmodule Discuss.Comment do
use Discuss.Web, :model
@derive {Poison.Encoder, only: [:content, :user]}
schema "comments" do
field :content, :string
belongs_to :user, Discuss.User
belongs_to :topic, Discuss.Topic
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:content])
|> validate_required([:content])
end
end
defmodule Discuss.Topic do
use Discuss.Web, :model
schema "topics" do
field :title, :string
belongs_to :user, Discuss.User
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title])
|> validate_required([:title])
end
end