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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
wintermeyer
That doesn’t work either:
Here just the changeset:
No worries. I am more than grateful that you type it on your phone. I know what a PITA that is.
frankdugan3
Looks like you’re missing something like
api: Animina.Accountsfrom yourfor_create.zachdaniel
This is advice for 3.0, as most 2.0 users will not have configured the
api: Apioption.For
Animina.Accounts.Reaction.create!(), that function is a code interface function which does not take a changeset. It takes the input directlywintermeyer
I want to call the
:likeaction which I defined here:If I just call
Animina.Accounts.Reaction.create!/1I get this error because thenameattribute is required.How can I call the
likeaction? I tried this but that didn’t work:zachdaniel
Ah, right, you don’t want to use the code interface function that you added for
:create, I see now.I believe that you mean to be calling the function on the api, not the resource. The
createfunction on the resource is defined by yourdefine :createin the code interface, and its meant to cal specifically the create action.wintermeyer
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
I opened a new question for this.
joelpaulkoch
Did you try to make the like action part of the code interface?
Something like:
And then call
Animina.Accounts.Reaction.create_likeor maybe it’sAnimina.Accounts.create_like. This way you don’t need the name argumentzachdaniel
You have a
:likeaction, 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
likeaction 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.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.
You can build a changeset and then call
YourApi.createnotYourApi.Resource.create. This is deprecated in 3.0You can build a changeset and then call
Ash.create. However, this only works if you have configured anapiwhen using the resource, i.euse 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.