Tag: bulkinsert

Rails:对集合而不是数据库表使用现有的模型validation规则

Rails 4,Mongoid而不是ActiveRecord(但是为了这个问题,这应该改变任何东西)。 假设我有一个带有一些validation规则的MyModel域类: class MyModel include Mongoid::Document field :text, type: String field :type, type: String belongs_to :parent validates :text, presence: true validates :type, inclusion: %w(ABC) validates_uniqueness_of :text, scope: :parent # important validation rule for the purpose of the question end 其中Parent是另一个域类: class Parent include Mongoid::Document field :name, type: String has_many my_models end 此外,我在数据库中的相关表填充了一些有效数据。 现在,我想从CSV文件导入一些数据,这可能与数据库中的现有数据冲突。 最简单的方法是为CSV中的每一行创建一个MyModel实例,并validation它是否有效,然后将其保存到数据库中(或丢弃它)。 […]

高效批量更新rails数据库

我正在尝试构建一个rake实用程序,它会经常更新我的数据库。 这是我到目前为止的代码: namespace :utils do # utils:update_ip # Downloads the file frim to the temp folder then unzips it in # Then updates the database. desc “Update ip-to-country database” task :update_ip => :environment do require ‘open-uri’ require ‘zip/zipfilesystem’ require ‘csv’ file_name = “ip-to-country.csv” file_path = “#{RAILS_ROOT}/db/” + file_name url = ‘http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip’ #check last time we […]

批量将记录插入Active Record表

我发现我的Model.create! 当我一次添加大量记录时,语句需要很长时间才能运行。 看看ActiveRecord-Import但是它没有使用哈希数组(这是我拥有的,我认为这很常见)。 如何提高性能?