firesidewing

firesidewing

Simple relationship managing with liveview

I’m warry of getting involved with Ash due to the abstraction it represents, but i wanted to give it a chance since it seems cool and interesting.

I have a couple of small side projects i manage and wanted to try to duplicate one of the simple ones (basically just CRUD with real time updates, a search function, and some user roles stuff). I’m getting stuck on the basics of managing relationships, I’m not sure how much “magic” to expect or what i need to be doing myself but i can’t seem to get the right incantation to get everything working. Granted, I haven’t been able to put much time into this, but I’ve been running through the docs and feel like i’m stuck in a loop.

I have two resources Contacts and Partners. Partners has a one to many with Contacts.

defmodule AshRevenueGenerator.Partners.Contact do
...
actions do
    defaults [:read, :destroy, update: :*]

    create :create do
      primary? true
      accept [:created_by_user_id]

      change relate_actor(:created_by_user)
    end
  end

...

relationships do
    belongs_to :partner, AshRevenueGenerator.Partners.Partner do
      allow_nil? false
      attribute_writable? true
      public? true
    end

    belongs_to :created_by_user, AshRevenueGenerator.Accounts.User do
      allow_nil? false
      attribute_writable? true
    end
  end
...
end

And for the form, I’m just using the one from the generator, with an added select input for the Partner. The public? true on the attributes and the relationship got me for a while, kind of annoying, would be nice if there was a short hand for it (might be, i haven’t be able to find it though). I’ve tried set_attribute in the action and adding the partner_id to the accepts, i’ve tried manage_relationship(:partner, :partner_id) but didn’t stick with that long. I can’t seem to get all the attributes to save for a contact and then to also be able to set the partner from the form at the same time. Current error the form is giving me is that partner is required (which is true but i’m passing in params with a partner_id set)

Long winded, but is there some sort of guide, or tutorial, or documentation I’m missing for this? is there a kind of convention for this simple CRUD or is it more just do whatever feels right? Also, apologies if this has already been asked or solved, i’ve been looking everywhere for a way to solve this and haven’t seen something on this basic level but this was kinda close

Marked As Solved

barnabasJ

barnabasJ

Ash Core Team

You only accept the created_by_user_id at the moment, you also need to accept the partner_id and you probably don’t need to accept the created_by_user_id attribute because you don’t pass that directly but use the actor to set the value.

accept [:created_by_user_id, :partner_id]

You don’t need manage_relationship if you set the ids directly.

There is a section in the getting started guide that shows this, it’s using an update action but otherwise it works the same way Get Started — ash v3.29.3

There is also this section about using manage_relationship Relationships — ash v3.29.3

You can also do everything manage_relationship and more using before/after action hooks.

Also Liked

firesidewing

firesidewing

Ah yeah, definitely remember reading those, that works just fine thank you!

Am I still able to use that :* trick in the accept parameters for an action, or the default_accept? It was handy to have but since my create/update actions handle a relationship i don’t think i can do the defaults [:read, :destroy, create: :*, update: :*]

barnabasJ

barnabasJ

Ash Core Team

I think it should work. :* stands for all public attributes, and the belongs_to definition adds a public attribute if the relationship is public.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
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
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
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

We're in Beta

About us Mission Statement