Create a resource including a belongs_to id
I have this resource with the name
location
:
Now I want to create a new entry for this resource with this code:
The recourse Location
gets created but the attribute level_id
is empty.
How can I achieve to create a Location
with the belongs_to
association?6 Replies
in your
create
action definition, you would use manage_relationship
to allow the setting of the level_id
- let me grab an example
In your Location resource:
this would allow the level_id
attribute to be set when creating a Location, ensuring it's also a valid value etc.Thanks @Rebecca Le !
For the archive. The function needs an addition line:
Withouth the
argument :level_id, :string
it doesn't work.You can also set
attribute_writable? true
on the relationship to make the attribute that it adds writable as a normal attribute.Much cleaner. Thank you Zach!
There are some differences in their behavior, for instance if you have policies on reading the related resource,
manage_relationship
will look it up first to make sure you can read it, that kind of thing.
but attribute_writable?
is simpler generally, yes. manage_relationship
would be required for every other type of relationship though since they don't have an associated attribute like belongs_to
does.TIL also, thanks 😄