简体   繁体   中英

How do I assign user_id and journal_id to the Deal model instance imported from excel (Rails)

I have implemented excel import (using spreadsheet and roo gems) in my rails app for Deal model.

Deal model belongs to User and Journal. I need to attribute current_user.id and @journal.id to each imported deal.

This is what I have now:

deals_controller.rb

def import
  @user = current_user
  Deal.import(params[:file])
  redirect_to journals_path, notice: "Deals imported."
end

deal.rb

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]
    deal = find_by_id(row["id"]) || new
    deal.attributes = row.to_hash.slice(*accessible_attributes)
    deal.save!
  end
end

def self.accessible_attributes 
    ['user_id', 'journal_id', 'date_in', 'date_out', 'ticker', 'type', 'open_price', 'close_price', 'quantity', 'revenue',] 
end

def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
  when ".csv" then Roo::Csv.new(file.path, nil, :ignore)
  when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
  when ".xlsx" then Roo::Excelx.new(file.path, options={})
  else raise "Unknown file type: #{file.original_filename}"
  end
end

But in this case users can access journals of other users by inputing their user_id and journal_id in excel file.

What is the best way to assign user_id and journal_id to each deal automatically while importing deals from excel file?

Just assign the values you want to assign to the model:

def self.import(file, user_id, journal_id)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    deal = find_by_id(row["id"]) || new
    deal.attributes = row.to_hash.slice(*accessible_attributes)
    deal.user_id = user_id
    deal.journal_id = journal_id
    deal.save!
  end
end

And in your controller, just pass in the needed parameters:

def import
  Deal.import(params[:file], current_user.id, @journal.id)
  redirect_to journals_path, notice: "Deals imported."
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM