sezaru

sezaru

Suggestion: Add possibility to write calculate expression inside code block

Currently, when writing a calculation, the third argument is the expression.

For example:

calculate :full_name, :string, expr(first_name <> " " <> last_name)

This looks clean and simple, but when the calculation is a little bit more complex, it starts to get a little bit messy:

    calculate :total_feedbacks_in_class_this_month,
              :integer,
              expr(
                count(feedbacks,
                  query: [
                    filter:
                      expr(
                        class_id == ^arg(:class_id) and
                          inserted_at >= fragment("date_trunc('month', now())") and
                          inserted_at <
                            fragment("date_trunc('month', now()) + interval '1 month'")
                      )
                  ]
                )
              ) do
      argument :class_id, :string, allow_nil?: false
    end

I was wondering if we could add an alternative way to write the calculations where we can not write the third argument and alternatively add it inside the calculate code block.

Something like this:

   calculate :total_feedbacks_in_class_this_month, :integer do
      expression expr(
        count(feedbacks,
          query: [
            filter:
              expr(
                class_id == ^arg(:class_id) and
                  inserted_at >= fragment("date_trunc('month', now())") and
                  inserted_at < fragment("date_trunc('month', now()) + interval '1 month'")
              )
          ]
        )
      )

      argument :class_id, :string, allow_nil?: false
    end

At least for me, this seems easier to read.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

You can do this today:

   calculate :total_feedbacks_in_class_this_month, :integer do
      calculation expr(
        count(feedbacks,
          query: [
            filter:
              expr(
                class_id == ^arg(:class_id) and
                  inserted_at >= fragment("date_trunc('month', now())") and
                  inserted_at < fragment("date_trunc('month', now()) + interval '1 month'")
              )
          ]
        )
      )

      argument :class_id, :string, allow_nil?: false
    end

Last Post!

zachdaniel

zachdaniel

Creator of Ash

I’m actually not sure if its documented anywhere…

Where Next?

Popular in Questions Top

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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement