kamaroly

kamaroly

How To Add Limit by and Order By In Ash Resource Action?

Hi,

I want to add actio to the reason for picking first record from the database that matches as specific condition in a specific order. Something similar to laravel eloquent first function. Flight::first();

I want to be able to call Flight.first() and get the first record in the table.

Here is what I tried to do with Ash but it is not working. It gives the error

** (Ash.Error.Invalid) Invalid Error

* No such field limit for resource Transports.Flight
    at filter
defmodule Transports.Flight do
  use Ash.Resource,
      domain: Transports,
      data_layer: AshPostgres.DataLayer

  require Ash.Query

  postgres do
    table "flights"
    repo Transports.Repo
  end

  actions do
    defaults [:create, :read, :update, :destroy]

    read :first do
      limit: 1
    end
  end
end

Marked As Solved

rbino

rbino

Ash Core Team

This is probably what you’re looking for:

read :first do
  prepare build(limit: 1)
end

Given that you’re returning the first result it’s a good idea to have a well-defined sort. You can do it in the same build like this:

read :first do
  prepare build(sort: :name, limit: 1)
end

Also Liked

kamaroly

kamaroly

EXACTLY!! that’s what I was looking for! Thanks a lot!

Where Next?

Popular in Questions Top

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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
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

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement