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
That query doesn’t seem unreasonable; what do you get when you pass it to Repo.to_sql/2?
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
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
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
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
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
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
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
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
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
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
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
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
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
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
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







