「我從一個CSV文件導入紀錄,需要添加的問題是我沒有任何標識的文件之前檢查現有的記錄查找帶有多個協會
這裏是車型。:
class Shooter < ActiveRecord::Base
has_many :scores
end
class Event < ActiveRecord::Base
has_many :shooters
has_many :scores
end
class Score < ActiveRecord::Base
belongs_to :shooter
belongs_to :event
end
這裏是我的導入方法:
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
# some_how_find_duplicates_here...
score.attributes = row.to_hash
score.save!
end
end
我猜我需要名稱進行搜索,並做一些事情,如:
score = Score.where("shooter.name == row.shooter_name & event.name == row.event_name") || new
任何方向將不勝感激。
這是獨特的領域? –
在這個階段,我正在驗證shooter.name和event.name的唯一性 – Kiwi