This project is archived and is in readonly mode.
ActiveRecord.clone does not clear out timestamps
-
Cesario
- Tag set to activerecord, patch
Here's a patch against master. It's forcing updated_at, updated_on, created_at and created_on to nil on the clone object.
-
Dan Pickett
- Tag changed from activerecord, patch to activerecord, bugmash, patch
-
Cesario
- Tag changed from activerecord, bugmash, patch to 3.0.0.beta3, activerecord, bugmash, patch
-
Christopher Redinger
+1
This patch applies cleanly. It's well tested. All tests passed.
-
Neeraj Singh
If clone method is going to wipe out timestamps then clone should accpet an option (:preserve_timestamps => true) that will preserve timestamps incase I need it.
-
pleax
-1
There is a problem with current patch with models that have no created_at/updated_at fields.
For that kind of models following test will fail:test "cloned model attributes" do token = Token.new # model without created_at/updated_at timestamps cloned_token = token.clone [:created_at, :updated_at].each do |key| assert_equal token.has_attribute?(key), cloned_token.has_attribute?(key) end end -
Cesario
Here's a more complete patch, that takes into account the comment made by pleax.
Neeraj, from what I've learned so far, AR::Base does not override
clone(ordup). It just overrides its specific behavior by defininginitialize_copy.So if one need to preserve the timestamps, this would need to be redefined by another specific
initialize_copymethod in one's own classes.Is this an acceptable solution? If not, I'll have to reintroduce
cloneand I'm almost sure it's a bad idea if I refer to ticket/patch #3164.Thanks for your feedbacks!
-
Jeff Kreeftmeijer
+1
Applies cleanly. All tests passing. :)
-
pleax
+1
With updated patch test-cases and implementation seem full and correct for me.
-
PacoGuzman
+1 All tests passing
-
Santiago Pastorino
I don't agree with doing that way i prefer the default behavior the way it is and an option (:preserve_timestamps => false)
-
Jeremy Kemper
- State changed from new to open
Perhaps
dupshould keep the timestamps (identical copy) andcloneshould not (new copy). -
Cesario
Jeremy:
dupis already preserving the timestamps (it's not usinginitialize_copy). -
Michael Raidel
+1
All ar-mysql- and -postgresql-tests pass (well, there are 2 failures for postgresql and 2 failures and 1 error for mysql, but they are the same without the path), patch applies cleanly, behaviour makes sense
-
Brian Underwood
- Importance changed from to Low
What happened to this ticket? I would love for this to change. I think the preserve_timestamps timestamps option is good, but I think it makes more sense for the timestamps not to be preserved by default.
-
Cesario
Brian, here are some follow-ups about this https://github.com/rails/rails/pull/113
-
Aaron Patterson
- State changed from open to resolved
I've applied Cesario's patches to master.
dupwill not preserve timestamps.
