kamaroly

kamaroly

How to Read Views columns that are not matching Resource attributes?

How to read VIEW columns from a custom statement that don’t exist as attribute on the resource without throwing a Ash.Error.Query.NoSuchAttribute

defmodule MyApp.Members.Member do
  use Ash.Resource,
    domain: MyApp.Members,
    data_layer: AshPostgres.DataLayer

  postgres do
    table "members"
    repo SocialFund.Repo

    custom_statements do
      statement :view_member_contributions do
        # Migrations up
        up "CREATE VIEW view_member_contributions
              AS SELECT
               members.member_number,
               contributions.amount AS contributed_amount,
               contributions.month AS period
            FROM members LEFT JOIN  contributions
            ON members.id = contributions.member_id;"

        # Migration down
        down "DROP VIEW IF EXISTS view_member_contributions;"
      end
    end
  end

  actions do
    read :contribution_report do
      prepare build(select: [:member_number, :contributed_amount, :period])
      prepare set_context(%{data_layer: %{table: "view_member_contributions"}})
    end
  end

 # Other definitions
end

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Probably not, TBH. It’s used for a whole bunch of things in Ash proper. You can remove the id from your resource entirely if needed, but not de-select it for a resource that has a primary key.

Also Liked

herrybrook

herrybrook

Hi kamaroly,
You can handle columns from a view that aren’t attributes on your resource by either defining virtual attributes marked as read_only: true or creating a custom read action that selects those extra columns. This way, Ash won’t throw a NoSuchAttribute error, but you can still access the data from your view.

kamaroly

kamaroly

Thanks. Please share an example code.

zachdaniel

zachdaniel

Creator of Ash

We always select the primary key. Can that not be added to the view?

Where Next?

Popular in Questions Top

New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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

We're in Beta

About us Mission Statement