wintermeyer

wintermeyer

I have a Reaction resource which is used to store likes and blocks in a social media context (a user can like an other user). I want to create a like action but get the following error. How can I fix this?

iex(12)> like = Animina.Accounts.Reaction 
|> Ash.Changeset.for_create(:like, %{sender_id: user2.id, receiver_id: stefan.id}) 
|> Animina.Accounts.Reaction.create!()
** (Protocol.UndefinedError) protocol Enumerable not implemented for #Ash.Changeset<action_type: :create, action: :like, attributes: %{sender_id: "207a829e-2464-42f7-8257-f4fa482fc7c2", receiver_id: "096da9ff-bcf7-49a1-8cba-f98413f37cab"}, relationships: %{},

The resource:

defmodule Animina.Accounts.Reaction do
  @moduledoc """
  This is the Reaction module which we use to manage likes, block, etc.
  """

  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

  attributes do
    uuid_primary_key :id

    attribute :name, :atom do
      constraints one_of: [:like, :block]
      allow_nil? false
    end

    create_timestamp :created_at
  end

  relationships do
    belongs_to :sender, Animina.Accounts.User do
      allow_nil? false
      attribute_writable? true
    end

    belongs_to :receiver, Animina.Accounts.User do
      allow_nil? false
      attribute_writable? true
    end
  end

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

    create :like do
      accept [:sender_id, :receiver_id]
      change set_attribute(:name, :like)
    end
  end

  code_interface do
    define_for Animina.Accounts
    define :read
    define :create
    define :destroy
    define :by_id, get_by: [:id], action: :read
    define :by_sender_id, get_by: [:sender_id], action: :read
    define :by_receiver_id, get_by: [:receiver_id], action: :read
  end

  identities do
    identity :unique_reaction, [:sender_id, :receiver_id, :name]
  end

  postgres do
    table "reactions"
    repo Animina.Repo

    references do
      reference :sender, on_delete: :delete
      reference :receiver, on_delete: :delete
    end
  end
end

Showing Posts 1 to 10

benswift

benswift

Looks like you are mixing up the changeset-based resource creation flow and the code interface one. Once you have a changeset, you just pipe it into ‘Ash.create!’.

Sorry about the terse reply, typing on my phone right now :slight_smile:

wintermeyer

wintermeyer OP

That doesn’t work either:

iex(3)> Animina.Accounts.Reaction 
|> Ash.Changeset.for_create(:like, %{sender_id: stefan.id, receiver_id: stefan.id}) 
|> Ash.create!

** (ArgumentError) No api configured for resource Animina.Accounts.Reaction.

Here just the changeset:

iex(3)> Animina.Accounts.Reaction 
|> Ash.Changeset.for_create(:like, %{sender_id: stefan.id, receiver_id: stefan.id}) 
#Ash.Changeset<
  action_type: :create,
  action: :like,
  attributes: %{
    name: :like,
    receiver_id: "16d7d94b-3975-4220-afc6-83d0ad009c9e",
    sender_id: "16d7d94b-3975-4220-afc6-83d0ad009c9e"
  },
  relationships: %{},
  errors: [],
  data: #Animina.Accounts.Reaction<
    receiver: #Ash.NotLoaded<:relationship>,
    sender: #Ash.NotLoaded<:relationship>,
    __meta__: #Ecto.Schema.Metadata<:built, "reactions">,
    id: nil,
    name: nil,
    created_at: nil,
    sender_id: nil,
    receiver_id: nil,
    aggregates: %{},
    calculations: %{},
    ...
  >,
  context: %{actor: nil, authorize?: false},
  valid?: true
>

Sorry about the terse reply, typing on my phone right now :slight_smile:

No worries. I am more than grateful that you type it on your phone. I know what a PITA that is.

frankdugan3

frankdugan3

Looks like you’re missing something like api: Animina.Accounts from your for_create.

zachdaniel

zachdaniel

Creator of Ash

This is advice for 3.0, as most 2.0 users will not have configured the api: Api option.

For Animina.Accounts.Reaction.create!(), that function is a code interface function which does not take a changeset. It takes the input directly

Animina.Accounts.Reaction.create!(%{sender_id: user2.id, receiver_id: Stefan.id})
wintermeyer

wintermeyer OP

I want to call the :like action which I defined here:

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

    create :like do
      accept [:sender_id, :receiver_id]
      change set_attribute(:name, :like)
    end
  end

If I just call Animina.Accounts.Reaction.create!/1 I get this error because the name attribute is required.

iex(5)> Animina.Accounts.Reaction.create!(%{sender_id: stefan.id, receiver_id: stefan.id})
** (Ash.Error.Invalid) Input Invalid

* attribute name is required
    (ash 2.18.2) lib/ash/api/api.ex:2711: Ash.Api.unwrap_or_raise!/3

How can I call the like action? I tried this but that didn’t work:

iex(5)> Animina.Accounts.Reaction.create!(:like, %{sender_id: stefan.id, receiver_id: stefan.id})
** (FunctionClauseError) no function clause matching in Keyword.split/2    
    
    The following arguments were given to Keyword.split/2:
    
        # 1
        %{
          receiver_id: "16d7d94b-3975-4220-afc6-83d0ad009c9e",
          sender_id: "16d7d94b-3975-4220-afc6-83d0ad009c9e"
        }
    
        # 2
        [:changeset, :actor, :tenant, :authorize?, :tracer]
    
    Attempted function clauses (showing 1 out of 1):
    
        def split(keywords, keys) when is_list(keywords) and is_list(keys)
    
    (elixir 1.16.0) lib/keyword.ex:1179: Keyword.split/2
    (animina 0.1.0) deps/ash/lib/ash/code_interface.ex:536: Animina.Accounts.Reaction.create!/2
zachdaniel

zachdaniel

Creator of Ash

Ah, right, you don’t want to use the code interface function that you added for :create, I see now.

like = Animina.Accounts.Reaction 
|> Ash.Changeset.for_create(:like, %{sender_id: user2.id, receiver_id: stefan.id}) 
|> Animina.Accounts.create!()

I believe that you mean to be calling the function on the api, not the resource. The create function on the resource is defined by your define :create in the code interface, and its meant to cal specifically the create action.

wintermeyer

wintermeyer OP

Now I am totally lost. What is the cleanest solution for the given problem? I want for the future external JSON-API a way to create a like the most secure way. What is the best way to do so?

wintermeyer

wintermeyer OP

I opened a new question for this.

joelpaulkoch

joelpaulkoch

Did you try to make the like action part of the code interface?

Something like:

code_interface do
...
define :create_like, action: :like
end

And then call Animina.Accounts.Reaction.create_like or maybe it’s Animina.Accounts.create_like. This way you don’t need the name argument

zachdaniel

zachdaniel

Creator of Ash

You have a :like action, and so how you call it in code can’t and won’t affect how the JSON:API behaves (which is why Ash is the way that it is).

There are three ways to call this like action in code, one of which is no longer available in 3.0. AshJsonApi will be unaffected by your choice here. You should choose #1, but I’m including 2 & 3 informationally.

  1. You can define a code interface function as @joelpaulkoch says. This is the recommended approach. For this, you’d define something like this:
define :like, args: [:sender_id, :receiver_id]

and call it like YourResource.like(sender_id, receiver_id).

NOTE: In 3.0 this can be done in one additional way, where you define the code interface on the domain. It’s not relevant here, see the upgrade guide for more.

  1. You can build a changeset and then call YourApi.create not YourApi.Resource.create. This is deprecated in 3.0

  2. You can build a changeset and then call Ash.create. However, this only works if you have configured an api when using the resource, i.e use Ash.Resource, api: Api. (because we have to know the api). This is essentially a compatibility feature to make upgrading to 3.0. Don’t bother with it right now. When you are interested in upgrading to 3.0, the upgrade guide will explain how to make this change.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews