Create record with association using prefix ids #82
-
New here, so apologies if this is covered somewhere else - did a search, dug into the code, and read old issues and did not find anything. We are using this library, and have a User model and a Company model. When we are trying to create a user with params like: We are getting an error: But it does exist, if we do: Is it possible to create a record with an associated object using a prefix-id? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Correct, you'd have to intercept that and decode it to the primary key ID. Your model can do that with something like this: def company_id=(value)
value.start_with?(_prefix_id.prefix) ? super(_prefix_id.decode(value)) : super
# Or with fallbacks enabled, should be able to just decode without the conditional:
# super _prefix_id.decode(value, fallback: true).first
end The more we override of ActiveRecord internals, the more issues arise so we've been trying to keep it minimal. |
Beta Was this translation helpful? Give feedback.
Correct, you'd have to intercept that and decode it to the primary key ID.
Your model can do that with something like this:
The more we override of ActiveRecord internals, the more issues arise so we've been trying to keep it minimal.