sahilpaudel

sahilpaudel

Getting latest live rule first then the rest of the data using Repo

I know it is a noob question but I can’t seem to figure it out.

I have 1000s of rules in the DB which has a boolean field as is_live and a DateTime field as start_time
A rule is considered a priority rule if it is the latest rule and it is live.

I want to write a Repo query where it returns all the rows first being the priority rule followed by live rule which is no longer priority and the is_live false rules

What I have in my function is as below

def all(%{"rule_set_id" => rule_set_id, "is_archived" => is_archived, "page" => page_number}) do
    page = from(rsv in @model,
      where: rsv.rule_set_id == ^rule_set_id and rsv.is_archived == ^is_archived,
      order_by: [desc: rsv.is_live, desc: rsv.start_time]
    )
    # |> Repo.all()
    # |> preload(@default_preloads)
    |> Repo.paginate(page: page_number, page_size: 5)

    {:ok, %{
           "rule_set_versions" => page.entries |> preload(@custom_preloads),
           "page_number" => page.page_number,
           "page_size" => page.page_size,
           "total_pages" => page.total_pages,
           "total_rows" => page.total_entries
          }
    }
  end

Note: I am using Scrivener Ecto for pagination.

First Post!

al2o3cr

al2o3cr

That query doesn’t seem unreasonable; what do you get when you pass it to Repo.to_sql/2?

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement