carlgleisner
Accepting attributes by different names than in the resource
Hi all,
I’m creating a resource that represents entities that can be either:
:naturalpersons or:legalpersons.
These have most attributes and actions in common and will primarily have to meet the need of having relationships along the lines of claim - belongs_to -> entity where the entity can be of either type. Of course, one would like to be able to query all entities and have their proper full name returned and so on.
I’m strongly suspecting that I should create a polymorphic relationship as described in the guides with an Entity resource and NaturalPerson and LegalPerson resources respectively, but a preliminary question has come to mind while evaluating the actual need for that.
If one were to have a resource Entity without that kind of polymorphism, could one have different :create actions that accept different attributes for the kinds of names that are then stored in :name1, :name2 and :name3 attributes on the Entity resource? Or would one “just” handle that in the UI?
:name1→:first_namefor:natural_person:s and:company_namefor:legal_person:s::name2→:middle_namefor:natural_person:s:name3→:last_namefor:natural_person:s
When for instance creating entities:s, I’d like to – somehow! – accept :first_name, :middle_name and :last_name in a :create_natural_person action and :company_name in a :create_legal_person action.
That does not feel very Ash:y however and I guess that it can lead to issues down the line.
As I noted above, I strongly suspect that this is a case of polymorphism.
Asking for a friend etc. ![]()
Thank you in advance good people!
Kind regards,
Carl
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Marked As Solved
zachdaniel
You can definitely do it with actions. Their job is to accept whatever inputs and encapsulate some given domain operation:
The actions guide and changes guide should have more examples,
set_attribute/2is a builtin but you can write your own.Also Liked
zachdaniel
I would say that it might be easier to handle this in your LiveView though, using
value={AshPhoenix.Form.value(form, :name1)}etc.You might need to only set the argument if its not already set for example.
carlgleisner
That’s really great, sometimes it’s simpler than one thinks – thank you!
Last Post!
carlgleisner
Got it working by only setting the argument – from data this time – if the argument is
nil.But there is still a validation issue that might deserve a new changset function
remove_error/3.This pre-populates the arguments and the
set_argument/2doesn’t get in the way of provided changes (someone was tired yesterday).But the thing is that the changeset has validation errors for all the required arguments from the get-go:
So when touching the form in AshAdmin the validation errors show even though the changeset reasonably should be valid.
Before touching:
After touching:
Maybe it would be hacky, but wouldn’t it be possible to have a
remove_error/3akin toadd_error/3? I noted thatadd_error/3also sets the changeset toinvalid– maybe it’s impossible to say whether it should be valid or invalid in that case? But iferrorsare empty after pre-populating the arguments one could perhaps dare to mark the changeset asvalid?