ruby-on-rails – Rails如何实例化从数据库中检索的对象?
发布时间:2020-12-17 01:29:45 所属栏目:百科 来源:网络整理
导读:在Rails中,ActiveRecord :: Base.new用于实例化尚未保存到数据库的新记录: new_user = User.new(name: "Bob")new_user.new_record? # = true 那么Rails如何实例化从数据库中检索的记录呢?它是否使用相同的新方法,然后在事后更改@new_record之类的值?或者
在Rails中,ActiveRecord :: Base.new用于实例化尚未保存到数据库的新记录:
new_user = User.new(name: "Bob") new_user.new_record? # => true 那么Rails如何实例化从数据库中检索的记录呢?它是否使用相同的新方法,然后在事后更改@new_record之类的值?或者它是否对从数据库中检索的记录使用某种特殊的实例化方法? 解决方法
new_record?方法可以在ActiveRecord框架中的active_record / persistence.rb中找到,它看起来像这样:
def new_record? @new_record end 然后,如果你在构造函数中查看active_record / core.rb,你会看到: def initialize(attributes = nil,options = {}) @attributes = self.class.initialize_attributes(self.class.column_defaults.deep_dup) @columns_hash = self.class.column_types.dup init_internals # here ensure_proper_type populate_with_current_scope_attributes assign_attributes(attributes,options) if attributes yield self if block_given? run_callbacks :initialize if _initialize_callbacks.any? end 如果我们在代码中深入挖掘一下: def init_internals pk = self.class.primary_key @attributes[pk] = nil unless @attributes.key?(pk) @aggregation_cache = {} @association_cache = {} @attributes_cache = {} @previously_changed = {} @changed_attributes = {} @readonly = false @destroyed = false @marked_for_destruction = false @new_record = true # here @mass_assignment_options = nil end 如您所见,默认情况下@new_record初始化为true. 但是,有些情况下@new_record属性设置为true,就像克隆记录时一样: user = User.first new_user = user.clone 这将调用initialize_dup方法,如下所示: def initialize_dup(other) # :nodoc: # Code removed @new_record = true # Code removed super end 或者当ActiveRecord从数据库中提取记录时.我不确定这部分,但我认为这个方法被称为: def init_with(coder) @attributes = self.class.initialize_attributes(coder['attributes']) @columns_hash = self.class.column_types.merge(coder['column_types'] || {}) init_internals @new_record = false run_callbacks :find run_callbacks :initialize self end 可以这样做: post = Post.allocate post.init_with('attributes' => { 'title' => 'hello world' }) 在第一个语句中,它在堆上分配内存空间而不像新的那样调用构造函数.然后它调用特殊构造函数init_with. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |