This project is archived and is in readonly mode.
autosaving associations skips validations and persists invalid data in the DB
-
Eloy Duran
- Assigned user set to Eloy Duran
-
MOROHASHI Kyosuke
+1
I also think this is very serious problem.
-
Eloy Duran
- State changed from new to invalid
I don't know whether a bug or intentional is this, but anyways
It's intentional. If you want to validate a single association, has_one/belongs_to, you will have to define the association with :validate => true. This has always been the case.
this may cause a dozen of serious problems on Rails 2.3, because we may easily create this situation automatically through nested_model_forms.
Nope it won't be a problem, because using NestedAttributes will automatically enable :autosave => true on the association. If :autosave is enabled an association should always be validated and saved.
If you find that not to be the case please file a failing test case.
Thanks
-
Eloy Duran
Btw: The source for AutosaveAssociation can be viewed here: http://github.com/rails/rails/bl...
-
Akira Matsuda
Nope it won't be a problem, because using NestedAttributes will automatically enable :autosave => true on the association. If :autosave is enabled an association should always be validated and saved.
Ahh, I see. So, this problem is not directly related to NestedAttributes. Sorry for the wrong point of argument.
But I'm still not totally agree with you. Please don't close the ticket.
Let me repeat the most important point.
any model could never be saved without validations except when save_without_validation method are explicitly called by the developer.
I tried the same operation on Rails 2.2.2, and found that on Rails 2.2.2, saving the pirate does not raise an error, and neither saves the invalid Ship model.
On the other hand, ActiveRecord 2.3 changes its behaviour to implicitly inject an invalid record into DB with normal :save method, which should never be happen.I really love the nested_form feature, but this AR matter is absolutely a worse change. Couldn't it rolled back to AR 2.2.2 behaviour?
-
Eloy Duran
- State changed from invalid to open
But I'm still not totally agree with you. Please don't close the ticket.
Sumimasen Matsuda-san.
I did not want to scare you into believing that it would not be fixed, just that the original ticket misled me… Now that you re-explained it some more it seems we are on the same page :)
Let me repeat the most important point. any model could never be saved without validations except when save_without_validation method are explicitly called by the developer. I tried the same operation on Rails 2.2.2, and found that on Rails 2.2.2, saving the pirate does not raise an error, and neither saves the invalid Ship model. On the other hand, ActiveRecord 2.3 changes its behaviour to implicitly inject an invalid record into DB with normal :save method, which should never be happen. I really love the nested_form feature, but this AR matter is absolutely a worse change. Couldn't it rolled back to AR 2.2.2 behaviour?
Ok I have checked the source of 2.2 again and I think I finally figured out why this regression was introduced. As you can see on this line it would indicate that a has_one/belongs_to only validates when :validate => true. However, this did not affect the saving of the record when the parent is a new record, in that case it did #save(true), so it did ran the validations:
http://github.com/rails/rails/bl...
Of course the biggest problem is that there apperantly was no test for this. So when I refactored it I apparently failed to grasp the original intent.
Too bad this only got noticed until now, so I guess we're too late for 2.3. But that's life, there was an RC and it was gone unnoticed, nothing that we can do about it now. I'll fix this on edge asap though.
Thanks
-
Eloy Duran
- Assigned user changed from Eloy Duran to Pratik
This should fix it. Thanks for the report.
-
Cody Fauser
+1
this fixed the issue we were having with has_one saving invalid child records when we upgraded to Rails 2.3
-
Jordan Brough
Thanks, this fixed this problem for us also.
-
José Valim
Just run into this problem. The patch fixed it.
Eloy, can you get it applied?
-
Tobias Lütke
We are running this as part of Shopify since the patch was posted. +1
-
Repository
- State changed from open to resolved
(from [da3c21ead59cb47b8f4c69c6bd95f225a9c8b479]) Ensure the parent record is always saved when the child is invalid. [#2249 autosaving associations skips validations and persists invalid data in the DB state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
-
Joshua Warchol
Just wanted to mention that a side effect of this bug is that before_validations callbacks are not called when doing build_ followed by a save on the parent. The patch fixes this. It's a shame it's not applied to the 2.3-stable branch. Adding :validate => true to the association, using before_save, or applying this patch to frozen rails gets around that problem.
Thank you all for identifying and fixing this issue!
-
Developers
It would be great if this could be applied to 2-3-stable. Like Tobi mentioned, we've been running this patch in production with Shopify since March 31.
-
Cody Fauser
Previous comment was from me. I was logged in with the wrong Lighthouse account.
-
Repository
(from [a70c78177a564c2f2cd09846a5e7ab6e8669e9f2]) Ensure the parent record is always saved when the child is invalid. [#2249 autosaving associations skips validations and persists invalid data in the DB state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/a70c78177a564c2f2cd09846a5e7ab...
