Publish :create, ["created", deeply_nested_item_id]
Let's say my resources have this relationship,
chat_room
has many post
s which then has many comment
s.
and I want to have this pub_sub
definedin comment.ex.
Unfortuantely, this gives me the following error. What's the right way to do this?
My chat_room_load()
function that feeds the data to the form is defined like this.
6 Replies
This one is interesting. I think what you might need to do is something a bit ugly, but basically two things need to happen.
1. the pub sub notifier is going to have to ignore not loaded values.
2. you'll need to load the value on anything before you change it and after you change it.
Thank you!
I tried these and :update works well, but not :create. Can you please see what I'm doing wrong?
In the :create action, I tried updating chat_room_id in changeset.data
and changeset.attributes, but neither works. (ie. same error message mentioned above)
The first IO.inspect message inside
after_action` prints, so that makes me think the after_action hook isn't invoked.for the create and update, you can just add
change load(:chat_room_id)
which will take care of the after action
TBH your best bet here though is probably a custom notifier
Alternatively we can make some changes to teach the notifier how to load things when they are calculations/aggregates, which also sounds reasonable but is work that needs to be doneThank you! Is the way I wrote after_action wrong? I would like to learn how to so it as it seems to be helpful elsewhere in my project.
I noticed if I remove
changeset |> IO.inspect(label: "changeset in create ###########")
at the end, after_action
does run and prints the output of IO.inspect(label: "changeset in create after action ###########")
However, it then gives this error. (probably because change
isn't returning a changeset?)
in your specific case, you aren't returning the change set with the after action hook
i.e
that throws away the change set that has the hook attached
the
after_action
function needs to return {:ok, result}
(or {:error, error}
)Thank you!
load(:chat_room_id)
worked.