2012年3月8日木曜日

Railsのcloneとdup

昔と今でActiveRecord#clone, ActiveRecord#dupの動作が入れ替わっている気が、
バージョン3.2.2で確認
Schedule.first.clone.id
=> 1
Schedule.first.dup.id
=> nil

こんなんだったっけ?逆じゃなかった?
CHANGELOG見ると
* ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed
to closer match normal Ruby dup and clone semantics.
* Calling ActiveRecord::Base#clone will result in a shallow copy of the record,
including copying the frozen state. No callbacks will be called.
* Calling ActiveRecord::Base#dup will duplicate the record, including calling
after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.

だって。ソース追いたいけど、定義場所どこかを調べるの大変だよなぁ。
source_locationとかでちゃちゃっといけないもんかね

0 件のコメント: