Answer the question
In order to leave comments, you need to log in
How to import csv file into mysql join table using rails?
There are three models Item Gost ItemGosts
class Item < ActiveRecord::Base
has_many :item_gosts
has_many :gosts, through: :item_gosts
end
class ItemGost < ActiveRecord::Base
belongs_to :item
belongs_to :gost
end
class Gost < ActiveRecord::Base
has_many :item_gosts
has_many :items, through: :item_gosts
end
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
item = Item.find_by_id(row["item_id"])
gosts = Gost.find_or_create_by(name: row["gosts_names"].split(;))
?????
item.save!
end
end
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question