Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用rails活动记录批量插入所有嵌套属性_Ruby On Rails_Postgresql_Ruby On Rails 4_Bulkinsert - Fatal编程技术网

Ruby on rails 如何使用rails活动记录批量插入所有嵌套属性

Ruby on rails 如何使用rails活动记录批量插入所有嵌套属性,ruby-on-rails,postgresql,ruby-on-rails-4,bulkinsert,Ruby On Rails,Postgresql,Ruby On Rails 4,Bulkinsert,我有一个这样的报告模型:- class Report < ActiveRecord::Base has_many :report_clients accepts_nested_attributes_for :report_clients, :reject_if => proc { |attributes| attributes['client_id'].blank? }, :allow_destroy => true end 它将运行1个查询以插入报表,2个查询以插

我有一个这样的报告模型:-

class Report < ActiveRecord::Base
  has_many :report_clients
  accepts_nested_attributes_for :report_clients, :reject_if => proc { |attributes| attributes['client_id'].blank? },  :allow_destroy => true
end
它将运行1个查询以插入报表,2个查询以插入报表\u客户端

通常,我会在每个报表中插入1000个report_客户端,这会导致1000个sql查询


我知道,我可以通过编写原始sql insert使用大容量插入来解决这个问题。但我想知道是否有其他方法/更好的方法来做到这一点

经历过类似的场景后,这类案例有一个令人敬畏的宝石

report = Report.new(name: params[:name])
report.save_with_nested_attributes(report_clients_attributes)
在report.rb中

“批量导入记录”中提到了许多其他方法


希望有帮助

您可以按如下方式使用create

Report.create({name: params[:name],  report_clients: report_clients})
其中,report_客户端定义为

def report_clients
        clients = [{client_id: 1}, {client_id:2}]
        clients.map do |client|
          ReportClient.new(client)
        end
end

感谢@RSB的帮助。在这种情况下,报表模型的验证是否有效。是的,它运行验证,您可以使用“保存”!或者在if save下添加嵌套的客户机。我在事务块下添加了所有内容来处理和其他数据库错误。当我要运行ReportClient.importreport\u clients\u对象时,它显示的是创建许多没有验证或回调的类
def save_with_nested_attributes(report_clients_attributes)
  report_clients_objects = []
  transaction do
    save!
    report_clients_attributes.each do |client_attributes|
      report_clients_objects << report_clients.new(client_attributes)      
    end
    ReportClient.import(report_clients_objects)
  end
end
Report.create({name: params[:name],  report_clients: report_clients})
def report_clients
        clients = [{client_id: 1}, {client_id:2}]
        clients.map do |client|
          ReportClient.new(client)
        end
end