Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 忽略CSV导入时已导入的记录_Ruby On Rails_Sqlite_Csv - Fatal编程技术网

Ruby on rails 忽略CSV导入时已导入的记录

Ruby on rails 忽略CSV导入时已导入的记录,ruby-on-rails,sqlite,csv,Ruby On Rails,Sqlite,Csv,我有一个简单的方法,每天手动导入事务数据的CSV文件,并将它们放入SQLite数据库 在导入包含以前导入的记录的文件时,我希望忽略导入这些记录。我如何才能最好地实现这一点?(我有一个名为“aka_reference”的字段,理论上应该是唯一的。) 型号: class Transaction < ActiveRecord::Base def self.import(file) CSV.foreach(file.path, headers: true) do |row|

我有一个简单的方法,每天手动导入事务数据的CSV文件,并将它们放入SQLite数据库

在导入包含以前导入的记录的文件时,我希望忽略导入这些记录。我如何才能最好地实现这一点?(我有一个名为“aka_reference”的字段,理论上应该是唯一的。)

型号:

class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            Transaction.create! row.to_hash
        end
    end
end
类事务
如果
aka_reference
是事务中的一个属性,那么很容易

Transaction.create!(row.to_hash) unless Transaction.find_by(aka_reference:  row.to_hash['aka_reference'])

如果
aka_reference
是事务中的一个属性,那么很容易

Transaction.create!(row.to_hash) unless Transaction.find_by(aka_reference:  row.to_hash['aka_reference'])

如果
aka_reference
是事务中的一个属性,那么很容易

Transaction.create!(row.to_hash) unless Transaction.find_by(aka_reference:  row.to_hash['aka_reference'])

如果
aka_reference
是事务中的一个属性,那么很容易

Transaction.create!(row.to_hash) unless Transaction.find_by(aka_reference:  row.to_hash['aka_reference'])

有两种可能性。您可以使用
find\u或\u create\u by
方法。像这样的

 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            Transaction.find_or_create_by row.to_hash
        end
    end
end
 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            data = row.to_hash
            Transaction.create!(data) unless Transaction.exists?(aka_reference: data[:aka_reference])           
        end
    end
end

有两种可能性。您可以使用
find\u或\u create\u by
方法。像这样的

 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            Transaction.find_or_create_by row.to_hash
        end
    end
end
 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            data = row.to_hash
            Transaction.create!(data) unless Transaction.exists?(aka_reference: data[:aka_reference])           
        end
    end
end

有两种可能性。您可以使用
find\u或\u create\u by
方法。像这样的

 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            Transaction.find_or_create_by row.to_hash
        end
    end
end
 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            data = row.to_hash
            Transaction.create!(data) unless Transaction.exists?(aka_reference: data[:aka_reference])           
        end
    end
end

有两种可能性。您可以使用
find\u或\u create\u by
方法。像这样的

 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            Transaction.find_or_create_by row.to_hash
        end
    end
end
 class Transaction < ActiveRecord::Base
    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            data = row.to_hash
            Transaction.create!(data) unless Transaction.exists?(aka_reference: data[:aka_reference])           
        end
    end
end

thx,更喜欢事务的语义。按解决方案查找或创建,但实际上无法使其工作(即导入相同的CSV x2,记录数量也加倍)thx,更喜欢事务的语义。按解决方案查找或创建,但实际上无法使其工作(即导入相同的CSV x2,记录数量也加倍)thx,首选事务的语义。按解决方案查找或创建,但实际上无法使其工作(即导入相同的CSV x2时,记录数量也加倍)thx,首选事务的语义。按解决方案查找或创建,但实际上无法使其工作(即导入相同的CSV x2时,记录数量也加倍)