Ruby on rails Rails活动记录效率:模型。在块内创建vs模型。创建(数组)
以下哪一项效率更高Ruby on rails Rails活动记录效率:模型。在块内创建vs模型。创建(数组),ruby-on-rails,activerecord,Ruby On Rails,Activerecord,以下哪一项效率更高 #Array is an array of hashes Array.each do |a| #some logic to clean up the record for creation Model.create!(a) end vs 通过检查,将对集合执行collect,然后递归调用create。在这种情况下,使用第一种方法会更有效,因为它使用更少的操作,并且只执行一次is\u a?(数组)检查 源代码: # File activerecord/lib/ac
#Array is an array of hashes
Array.each do |a|
#some logic to clean up the record for creation
Model.create!(a)
end
vs
通过检查,将对集合执行collect,然后递归调用create。在这种情况下,使用第一种方法会更有效,因为它使用更少的操作,并且只执行一次is\u a?(数组)
检查
源代码:
# File activerecord/lib/active_record/base.rb, line 504
def create(attributes = nil, options = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, options, &block) }
else
object = new(attributes, options)
yield(object) if block_given?
object.save
object
end
end
# File activerecord/lib/active_record/base.rb, line 504
def create(attributes = nil, options = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, options, &block) }
else
object = new(attributes, options)
yield(object) if block_given?
object.save
object
end
end