我有三个模型,分别是经销商,城市和州,我在经销商和城市之间有一对多的关系。其次,城市与州之间存在一对多的关系:
class Dealer < ActiveRecord::Base
belongs_to :city
end
class City < ActiveRecord::Base
belongs_to :state
has_many :dealers
end
class State < ActiveRecord::Base
has_many :cities
end
在dealers_controller.rb中我有一个方法:
def import
Dealer.import(params[:file])
redirect_to dealers_path, notice: "Products imported."
end
这将参考经销商模型中的方法:
def self.import(file)
spreadsheet = open_spreadsheet(file)
city = City.new
state = State.new
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
dealer = find_by_id(row["id"].to_i) || new
dealer.dealerName = row["DEALERSHIPNAME"]
dealer.address = row["ADDRESS"]
dealer.pincode = row["PINCODE"]
city.name = row["CITY"]
state.name = row["STATE"]
dealer.phoneNo = row["PHONENO"]
dealer.mobileNo = row["MOBILE"]
city.save!
state.save!
dealer.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when '.xls' then Roo::Excel.new(file.path)
when '.xlsx' then Roo::Excelx.new(file.path)
else raise "Unknown file type: #{file.original_filename}"
end
end
问题是经销商数据完全存储在经销商表中,但城市和州一直存储只有最后一条记录且经销商表中的city_id为空且city表中的state_id也为空。
如何使用此经销商模型在城市和州表中上传城市和州的全部数据?
我们如何上传excel文件,该文件将填充一对多关联中的所有三个表?
答案 0 :(得分:0)
i was creating the city and states object once outside of the loop but its instance must be created for every row which is coming from excel sheet, stores states unique and stores city according to states
def self.import(file)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
dealer = find_by_id(row["id"].to_i) || new
dealer.dealerName = row["DEALERSHIPNAME"]
dealer.address = row["ADDRESS"]
dealer.pincode = row["PINCODE"]
dealer.phoneNo = row["PHONENO"]
dealer.mobileNo = row["MOBILE"]
stateName = row["STATE"]
state = State.where(:name=>stateName).first
state_id = state.id unless state.nil?
name = row["CITY"]
city = City.where(:name=>name).first
city_id = city.id unless city.nil?
if state.nil?
stateObj = State.new
stateObj.name = stateName
stateObj.save!
if city.nil?
cityObj = City.new
cityObj.name = name
cityObj.state_id = stateObj.id
cityObj.save!
dealer.city_id = cityObj.id
dealer.save!
else
cityObj = City.new
cityObj.name = city.name
cityObj.state_id = stateObj.id
cityObj.save!
dealer.city_id = cityObj.id
dealer.save!
end
else
if city.nil?
cityObj = City.new
cityObj.name = name
cityObj.state_id = state.id
cityObj.save!
dealer.city_id = cityObj.id
dealer.save!
else
unless City.where(:name=>name).map{|c|c.state_id}.include? state.id
cityObj = City.new
cityObj.name = city.name
cityObj.state_id = state.id
cityObj.save!
dealer.city_id = cityObj.id
dealer.save!
else
dealer.city_id = City.where(:name=>name,:state_id =>state.id).first.id
dealer.save!
end
end
end
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when '.xls' then Roo::Excel.new(file.path)
when '.xlsx' then Roo::Excelx.new(file.path)
else raise "Unknown file type: #{file.original_filename}"
end
end