This project is archived and is in readonly mode.
Record Cloning Not Shallow
-
Paul Gillard
- Tag changed from patch activerecord, clone to patch activerecord, clone, patch
I attach two patches. One which clears the association cache when cloning and so returning cloning to being genuinely shallow. The other which removes an incorrect (after reading http://dev.rubyonrails.org/ticket/7191) comment added in #3164.
-
Paul Gillard
- Assigned user set to josh
Josh, I hope you don't mind me assigning this to you? Koz assigned a previous ticket I worked on regarding cloning to you (#3164).
-
José Valim
- Milestone cleared.
- Assigned user changed from josh to José Valim
-
José Valim
- Milestone cleared.
-
Repository
- State changed from new to resolved
(from [d132dd33520ba61f7bfa9ba6fdd1b7b2bebd27f3]) Don't clone associations [#4894 state:resolved]
Cloning an active record object should be shallow in that it should copy attributes but not associations. This was no longer true as a result of #3164.
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/d132dd33520ba61f7bfa9ba6fdd1b7... -
Paul Gillard
The foreign key attribute doesn't get cleared just the instance variable on the object which contains the associated object. An (imagined) IRB session might be:
post = Post.first post.user_id # 1 post.instance_variable_get("@user") # nil post.user # #<User id: 1, name: "Paul"> post.instance_variable_get("@user") # #<User id: 1, name: "Paul"> clone = post.clone clone.user_id # 1 clone.instance_variable_get("@user") # nil clone.user # #<User id: 1, name: "Paul"> clone.instance_variable_get("@user") # #<User id: 1, name: "Paul"> post.clear_association_cache post.instance_variable_get("@user") # nil -
José Valim
Should I revert this current commit? Or a new patch is coming soon?
-
Paul Gillard
Sorry for the confusion. My comments were simply to explain to Kane how my patch works. Its not an indication that there is a problem with the patch. I'm happy that the patch you've committed is correct and should be left as it is.
If you're happy to, could you also commit my second patch? It removes a comment I added in #3164 which is no longer relevant.
-
José Valim
Paul, I forgot to tell, the second patch does not apply anymore. :(
-
Paul Gillard
Second patch to remove incorrect comment re-based against master and attached.
-
José Valim
FYI: the patch above was already applied. :)
-
Andrea Campi
- Tag changed from patch activerecord, clone, patch to activerecord, clone, patch
- Importance changed from to Low
bulk tags cleanup
