srcoulombe

srcoulombe

Struggling to use a Flop join_field for sorting

This might be somewhat related to Sorting by a join field in a query with Flop not working, but I wasn’t able to follow the author’s solution

Context

I have two schemas: one for assets and another for issues.

Assets
# asset.ex
@derive {
  Flop.Schema,
  filterable: [:name],
  sortable: [:updated_at, :ongoing_issues_count],
  adapter_opts: [
    join_fields: [
      ongoing_issues_count: [
        binding: :ongoing_issues_count,
        field: :count,
        ecto_type: :integer
      ]
    ]
  ]
}

schema "assets" do
  field :name, :string
  
  has_many :issues, Issue
end
Issues
# issue.ex
schema "issues" do
  field :title, :string
  field :description, :string
  field :status, Ecto.Enum, values: @supported_statuses, default: :ongoing
end

Intentions

I’m trying to do two things within the Flop table that lists all my assets:

  1. show how many issues with status==:ongoing are associated to each asset, and
  2. allow users to sort the table in increasing/decreasing order based on how many issues with status==:ongoing are associated to each asset

I was able to accomplish 1. by defining the @derive Flop.Schema in the “assets.ex” file:

image

I’m struggling to understand why I can’t sort by the ongoing_issues_count join field though :confused:

Here’s the list_assets function I’m currently working with:

def list_assets(params) do
    ongoing_issues_count_query =
      Issue
      |> where([issue], parent_as(:asset).id == issue.asset_id)
      |> where([issue], issue.status == :ongoing)
      |> select([issue], %{count: count(issue)})

    query =
      Asset
      |> from(as: :asset)
      |> join(
        :left_lateral,
        [asset],
        ongoing_issues_count in subquery(ongoing_issues_count_query),
        as: :ongoing_issues_count
      )
      |> select([asset, ongoing_issues_count], %{
        asset: asset,
        ongoing_issues_count: ongoing_issues_count
      })

    Flop.validate_and_run(query, params, for: Asset)
  end

Thanks in advance to anyone who reads this!

Marked As Solved

srcoulombe

srcoulombe

figured it out - i had one obvious bug, and one smaller mistake.

the smaller mistake was with how i had constructed the ongoing_issues_count_query. I ended up converting it to:

ongoing_issues_count_query =
      Issue
      |> where([issue], parent_as(:asset).id == issue.asset_id)
      |> where([issue], issue.status == :ongoing)
      |> group_by([issue], issue.asset_id)
      |> select([issue], %{asset_id: issue.asset_id, count: count(issue)})

with the outer query being:

query =
      Asset
      |> from(as: :asset)
      |> join(
        :left_lateral,
        [asset],
        ongoing_issues_count in subquery(ongoing_issues_count_query),
        as: :ongoing_issues_count,
        on: ongoing_issues_count.asset_id == asset.id
      )
      |> select([asset, ongoing_issues_count], %{
        asset: asset,
        ongoing_issues_count: coalesce(ongoing_issues_count.count, 0)
      })

the coalesce is there to ensure that null values are converted to 0.

the bigger bug was that my Flop table had

  <:col :let={%{asset: asset, ongoing_issues_count: ongoing_issues_count}} label="Ongoing issues">

instead of

  <:col :let={%{asset: asset, ongoing_issues_count: ongoing_issues_count}} label="Ongoing issues" field={:ongoing_issues_count}>

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

We're in Beta

About us Mission Statement