New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
accepts_nested_attributes_for does not work with non id
primary_key
#48714
Comments
I think, that the update_posts_data = {
'id' => post.id,
'comments_attributes' => [
{
'id' => Comment.first.uuid,
'name' => 'Even Better'
},
{
'name' => 'Another comment'
}
]
} To avoid confusion, we can also pretty easily change the logic to accept the actual primary key name as a key in the attributes ( |
I was able to get around it with But ordinal problem is still remain. From solution stand point I see that it can be solved:
Maybe other solutions. |
Could you elaborate on the need of this? So whenever you do I'm curious what |
As I understand, the
The proposed solution is to accept |
That what I was suspecting as well
To be honest this sounds like a valid expectation and I think it should be fixable in a non-breaking way by tweaking I would even say that this line is design-wise wrong This line tries to compare Changing it to |
Right, I was slightly wrong in my comment above, we shouldn't be substituting but rather extending the logic to check for the dynamic |
Yes, agreed, so please let me know if you want to open a PR. |
I don't have spare time to have a look this week but I can try looking into it next week unless you or someone else wants to work on that! I don't mind at all, to me this should be a very low risk change that delivers a quick win |
Hey! Any new on that issue? 🙂 I see the PR is opened but looks like dormant at the moment |
Steps to reproduce
Currently
accepts_nested_attributes_for
when it's trying to find existing records in relation it's hardcoded to take a look onid
column.Problem is that
id
is not alway a primary key. In our case we have one of tables does not haveid
column at all but usesuuid
is primary key.We've set
self.primary_key = 'uuid'
butaccepts_nested_attributes_for
does not uses this value to use in matching existing records.Expected behavior
Existing records are updated and new created.
Actual behavior
It always trying to create new records and hit violation on primary key:
System configuration
Rails version: 7.1.0.alpha
But present in any Rails version
Ruby version: 3.1.2
But present in any Ruby version
The text was updated successfully, but these errors were encountered: