maz

maz

Using Flop I want to return a list of tuples containing a Membership along with associated User.email

membership.ex:

  @derive {
    Flop.Schema,
    filterable: [:member_email],
    sortable: [:member_email],
    join_fields: [member_email: [binding: :joined_user, field: :email]]
  }

  @role_options ~w(admin member)a

  schema "orgs_memberships" do
    field :role, Ecto.Enum, values: @role_options
    field :is_inactive, :boolean, default: false
    ...
    belongs_to :user, User
    belongs_to :org, Org


  def all_by_org_joined(%Org{} = org) do
    joined = from(m in __MODULE__,
      join: u in assoc(m, :user),
      as: :joined_user,
      join: o in assoc(m, :org),
      on: o.id == ^org.id
    )

    dbg(joined)

    result = joined
    |> Markably.Repo.all()

    dbg(result)

result prints as but seems to be missing member_email:

result #=> [
  %Markably.Orgs.Membership{
    __meta__: #Ecto.Schema.Metadata<:loaded, "orgs_memberships">,
    id: "1eda8b14-bf24-6736-85c7-a1d5647b6bd8",
    role: :admin,
    is_inactive: false,
    user_id: "1eda8b14-be9c-6822-8ab9-61e2ff5b420b",
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    org_id: "1eda8b14-bf22-6d6e-b325-ea3ac7994ddf",
    org: #Ecto.Association.NotLoaded<association :org is not loaded>,
    inserted_at: ~N[2023-02-09 19:38:15],
    updated_at: ~N[2023-02-09 19:38:15]
  },
  %Markably.Orgs.Membership{
...

Am I doing this right?

flop 0.19.0
ecto 3.9.4
elixir 1.14.2

regards, Michael

Showing Posts 1 to 4

woylie

woylie

You’re missing a select. Flop only adds where and order by clauses, it won’t add a select for you.

maz

maz OP

Thanks for the response, it was helpful but there is one caveat: if I select like this I will be returning as a map, the Petal Data Table(Install) needs a struct(and also obviates the need for Flop, of course):

    joined = from(m in __MODULE__,
      join: u in assoc(m, :user),
      as: :joined_user,
      join: o in assoc(m, :org),
      on: o.id == ^org.id,
      select: %{id: m.id, first_name: u.first_name, last_name: u.last_name, email: u.email, is_inactive: m.is_inactive}
    )

How do I select into a Membership struct that has the member_email along with it?

jswanner

jswanner

You’re currently explicitly selecting a map, see Ecto.Query.API — Ecto v3.14.0 for some ways to select a struct.

maz

maz OP

Thanks, that helped.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews