Ecto has_one without own_to in Phoenix

I have a model Homethat contains content for a homepage, such as intro_copy, about_imageand about_copy.

In the model HomeId would also like to include 3 posts from my model Postusing the link has_one. Basically just binding them using an identifier.

My diagram is Homeas follows:

schema "home" do
  field :intro_copy, :string
  field :about_copy, :string
  field :about_image, Image.Type

  has_one :post_1, Post
  has_one :post_2, Post
  has_one :post_3, Post

  timestamps()
end

My function changesetlooks like this:

def changeset(struct, params \\ %{}) do
  struct
  |> cast_assoc(params, [:post_1, :post_2, :post_3])
  |> cast(params, @required_fields, @optional_fields)
end

In addition, in my migrations :home, the following rows are added to the table :

add :post_1_id, references(:posts)
add :post_2_id, references(:posts)
add :post_3_id, references(:posts)

Somewhere here Im obviously going wrong?

+4
source share
1 answer

home posts, home belongs_to Post. has_one - , posts , home.

:

has_one :post_1, Post
has_one :post_2, Post
has_one :post_3, Post

belongs_to :post_1, Post
belongs_to :post_2, Post
belongs_to :post_3, Post

, .

+5

Source: https://habr.com/ru/post/1671171/


All Articles