Method: ActiveRecord::Core#init_with
- Defined in:
- activerecord/lib/active_record/core.rb
#init_with(coder, &block) ⇒ Object
Initialize an empty model object from coder. coder should be the result of previously encoding an Active Record model, using #encode_with.
class Post < ActiveRecord::Base
end
old_post = Post.new(title: "hello world")
coder = {}
old_post.encode_with(coder)
post = Post.allocate
post.init_with(coder)
post.title # => 'hello world'
490 491 492 493 494 |
# File 'activerecord/lib/active_record/core.rb', line 490 def init_with(coder, &block) coder = LegacyYamlAdapter.convert(coder) attributes = self.class.yaml_encoder.decode(coder) init_with_attributes(attributes, coder["new_record"], &block) end |