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
When updating has_many relation via attributes it fails with either ActiveRecord::RecordNotFound or ActiveRecord::RecordNotUnique #468
Comments
Hard to tell. Did you setup the model correctly? What's the traceback? |
The traceback is not very enlightening I guess. I think I set up the model correctly? It does work when I do (paraphrased)
|
Same issue for me. I have this model which does NOT have a composite primary key
Its children do however.
I get the same error as in the first comment with the not found. |
Well, I managed to work around it. Here's how: First of all, on the child model, put required: false on the belongs_to. Secondly, and most importantly, Rails is simply not creating the hidden id attribute correctly. In other words, the nested parameters would normally pass the id of the child record to the controller so update_attributes on the parent would work. Rails is setting the value as "id1 id2" in the hidden. Like this:
What I did was I changed my fields_for to no longer produce the id, with include_id: false
and then construct it yourself, EXCEPT, instead of a space between the composite keys, use a comma, so:
Composite keys now correctly uses the id parameter for the lookup. Hope this helps someone else. |
Thanks for digging, I actually saw something similar recently and had to fix a method in active view that generated that id field. In the end I reverted the change in the project I was working on. I guess this could be added to CPK, although then CPK needs a reference to ActiveView for a one line code change. |
Do you remember what method it's calling? I'd have assumed |
Sorry, its been a long time at this point and I don't remember. I don't really know what to do about this. We could fix ActiveView but do we really want to pull that in as dependency? I guess we could have an overridden method but not actually load it, and then its up to the user to load it. @codeodor - Since I'm not using Rails anymore I'll leave it up to you if you want to go down that route or close this a known issue but one that won't be fixed (would be good to document it though). |
Given the age of this issue, it's likely stale and should be closed without a test case that can make a current version fail |
Yes, agreed. |
I can confirm the issue is still present in |
@dvergeylen if you have a test case, seems worth opening a new issue and linking this one |
it fails with
When including a
id
key as generated by formtastic's nested forms, i.e.it fails with
when using
"id" => "1,2"
or"id" => [1,2]
I getRecordNotUnique
again.Am I missing something on how composite_primary_keys is supposed to be used?
The text was updated successfully, but these errors were encountered: