dmitriid

dmitriid

Query result to changeset: invalid input

At one point I’ll wrap my head around the new changes to resource attributes in Ash, I promise :slight_smile:

Meanwhile here’s an issue I have:

module Quire.Blog.Page do
  actions do
    defaults [
      :read,
      :destroy,
      create: [
        :title,
        :slug,
        :raw,
        :rendered,
        :is_published,
        :published_at,
        :is_tag,
        :toc,
        :settings,
        :is_archived,
        :is_private,
        :show_on_timeline,
        :language
      ]
    ]
    action :duplicate, :struct do
      argument :page_id, :uuid
      transaction? true

      run fn query, _context ->
        id = query.arguments.page_id

        {:ok, page} =
          __MODULE__.get(id)
          |> Ash.load([:media, :tags, :pages, :external_scripts])

        new_page = %{
          page
          | id: nil,
            is_published: false,
            published_at: nil,
            slug: nil,
            is_private: false
        }

        {:ok, new_page} = new_page |> Map.from_struct() |> __MODULE__.create()
        ...
    end
end

This action will fail in create because the input will have quite more fields than just listed attributes: it will have all the calculated fields, and all the aggregations etc.

This worked in Ash 2.x, but now fails in Ash 3.0 with %Ash.Error.Invalid.NoSuchInput for every calculation, aggregate etc. in the provided map.

Is their a fast way to convert this into a proper changeset with inputs. E.g. by filtering only on keys available to the action?

Most Liked

dmitriid

dmitriid

I’ve solved it by

        new_page =
          page
          |> Map.from_struct()
          |> Map.take(@create_attributes)

        new_page = %{
          new_page
          | is_published: false,
            published_at: nil,
            slug: nil,
            is_private: false
        }

        {:ok, new_page} = new_page |> __MODULE__.create()

where @create_attributes contains all the necessary fields

zachdaniel

zachdaniel

Creator of Ash

You can also tell Ash what keys to ignore in cases that they are missing:

inputs = new_page |> Map.from_struct()
{:ok, new_page} =  __MODULE__.create(inputs, skip_unknown_inputs: Map.keys(inputs))

With that said, being more explicit as you are showing is always better :+1:

Where Next?

Popular in Questions Top

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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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

We're in Beta

About us Mission Statement