2015-06-03 64 views
-2

我有三種模式,分別是經銷商,城市和州,我在這裏有經銷商和城市之間的一對多關係。其次,有一個城市和國家之間的多對多關係:如何使用rails4中的關聯從excel上傳數據

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爲空,而且c_中的state_id ty表也是null。

如何使用此經銷商模型上傳城市和州表中的城市和州的全部數據?

我們如何上傳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 
相關問題