elt547

elt547

Many of my schemas can be considered to have a Feed, so I followed the many-to-many recommendation for polymorphic associations.

I want to be able to list feed items, create posts of different types, delete posts of different types from a single interface. My initial thought was to make a Feed protocol, so I’ve been going down this path:

defmodule Hydroplane.Feeds do
  alias Hydroplane.Feeds.Feed

  def list_feed_items(feedable) do
    Feed.list_posts(feedable) ++ Feed.list_polls(feedable)
  end

  def create_post(feedable, author, attrs) do
    Feed.create_post(feedable, author, attrs)
  end
end

And the Feed protocol is here:

defprotocol Hydroplane.Feeds.Feed do
  def list_posts(feedable)
  def list_polls(feedable)
  def create_post(feedable, author, attrs)
end

defimpl Hydroplane.Feeds.Feed, for: Hydroplane.Initiatives.Initiative do
  import Ecto.Query
  alias Ecto.Multi

  alias Hydroplane.Repo
  alias Hydroplane.Initiatives.Initiative
  alias Hydroplane.Feeds.{Post, Poll}

  def list_posts(initiative) do
    query =
      from(p in Post,
        join: ip in "initiative_posts",
        on: p.id == ip.post_id,
        join: i in Initiative,
        on: i.id == ip.initiative_id,
        where: i.id == ^initiative.id
      )

    query
    |> Repo.all()
    |> Repo.preload(:author)
  end

  def list_polls(initiative) do
    query =
      from(p in Poll,
        join: ip in "initiative_polls",
        on: p.id == ip.poll_id,
        join: i in Initiative,
        on: i.id == ip.initiative_id,
        where: i.id == ^initiative.id
      )

    query
    |> Repo.all()
    |> Repo.preload(:author)
  end

  def create_post(initiative, author, attrs) do
    result =
      Multi.new()
      |> Multi.insert(:post, Post.changeset(%Post{author: author}, attrs))
      |> Multi.insert(:association, fn %{post: post} -> end)
      |> Repo.transaction()
      # Stuff like this is where it can start to get really messy
  end
end

The absolute JANK that I’m creating, basically having to fully implement Feed for each feedable item makes me think this is the wrong approach. It seems crazy to implement everything differently when the only thing that changes is how I interact with the join table.

In Ruby the obvious way to solve this would be through metaprogramming, is that the proper route here? What’s a better way to do this? I’m sure there are several.

Showing Posts 1 to 2

al2o3cr

al2o3cr

Assuming the Initiative schema has the needed many_to_many associations, these functions could be shorter:

  def list_posts(initiative) do
   Ecto.assoc(initiative, :posts)
    |> Repo.all()
    |> Repo.preload(:author)
  end

  def list_polls(initiative) do
    Ecto.assoc(initiative, :polls)
    |> Repo.all()
    |> Repo.preload(:author)
  end

You could try to DRY this out further, but presumably Poll and Post are different somehow that might require different preloads.

Ecto.build_assoc sounds promising for the last part.

elt547

elt547 OP

Thanks, that’s actually a big help for the list functions. I can actually get away without the protocol now.

Do you have any idea how to do the same for build_assoc with a many to many association without resorting to a protocol? When I call build_assoc, it only builds a blank post record. I end up having to add the record in the initiative_posts join table manually using insert_all in an Ecto.Multi transaction.

Below is what I have for the Feeds context now. Way more slick, so thanks for that!

defmodule Hydroplane.Feeds do
  import Ecto.Query

  alias Hydroplane.Repo
  alias Hydroplane.Feeds.Feed

  def list_feed_items(feedable), do: list_posts(feedable)

  def list_posts(feedable) do
    Ecto.assoc(feedable, :posts)
    |> order_by_date()
    |> Repo.all()
    |> Repo.preload(:author)
  end

  def list_polls(feedable) do
    Ecto.assoc(feedable, :polls)
    |> order_by_date()
    |> Repo.all()
    |> Repo.preload(:author)
  end

  def create_post(feedable, author, attrs) do
    case Feed.create_post(feedable, author, attrs) do
      {:ok, %{post: post}} -> {:ok, post}
      _ -> {:error, "Could not create post"}
    end
  end

  defp order_by_date(query) do
    from p in query, order_by: [desc: p.inserted_at]
  end
end

Below is the protocol implementation for Initiative, which I’m not a fan of. I’d much rather be able to automatically create the record in the join table when I insert the post.

defimpl Hydroplane.Feeds.Feed, for: Hydroplane.Initiatives.Initiative do
  alias Ecto.Multi
  alias Hydroplane.Feeds.Post
  alias Hydroplane.Repo

  def create_post(initiative, author, attrs) do
    Multi.new()
    |> Multi.insert(:post, Post.changeset(%Post{author: author}, attrs))
    |> Multi.run(:initiative_post, fn _repo, %{post: post} ->
      insert_post_join(initiative, post)
    end)
    |> Repo.transaction()
  end

  defp insert_post_join(initiative, post) do
    case Repo.insert_all("initiative_posts", [%{initiative_id: initiative.id, post_id: post.id}]) do
      {1, _} -> {:ok, "Associated post with initiative"}
      _ -> {:error, "Unable to associate post with initiative"}
    end
  end
end

Is there any way that I can dynamically build a changeset that inserts two records, one in post and one in the join table the many-to-many association uses?

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews