我试图导入一些CSV文件到我的Rails应用程序。我学习并设法将表格导入到模型中而无需关联。CSV数据导入到Rails应用程序,使用其他协议“ID”
现在我已经设法将数据导入到具有关联的表格中,但只能在CSV列中输入实际的“id”编号。虽然功能强大,但这不是一个真正的选择,因为我有很多带有数千个ID的表。
我的主要目标是能够使用CSV中的列并输入实际值(与其关联的其他模型中存在的值)而不是id号。
我有一个国家模型和端口模型。端口模型与COUNTRY_ID
端口模型
class Port < ApplicationRecord
def self.import(file)
#code
CSV.foreach(file.path, headers: true) do |row|
port = find_by_id(row["id"])
Port.create! row.to_hash
end
end
belongs_to :shipment_type
belongs_to :country
has_many :origins, :class_name => 'Rate'
has_many :destinations, :class_name => 'Rate'
end
国家模型相关
class Country < ApplicationRecord
def self.import(file)
#code
CSV.foreach(file.path, headers: true) do |row|
Country.create! row.to_hash
end
end
has_many :ports, dependent: :destroy
end
schema.db
create_table "ports", force: :cascade do |t|
t.string "name"
t.string "port_code"
t.integer "shipment_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "country_id"
t.index ["country_id"], name: "index_ports_on_country_id", using: :btree
t.index ["shipment_type_id"], name: "index_ports_on_shipment_type_id", using: :btree
end
create_table "countries", force: :cascade do |t|
t.string "name"
t.string "country_code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "shipment_types", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
该协会正在工作,因为我能够以手动添加他们认为我创造的形式很好。
<%= form_for(port) do |f| %>
<% if port.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(port.errors.count, "error") %> prohibited this port from being saved:</h2>
<ul>
<% port.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :port_code %>
<%= f.text_field :port_code %>
</div>
<div class="field">
<%= f.label :shipment_type_id %>
<%= f.collection_select :shipment_type_id, ShipmentType.all, :id, :name %>
</div>
<div class="field">
<%= f.label :country_code %>
<%= f.collection_select :country_id, Country.all, :id, :country_code %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
任何指导或帮助将不胜感激。现在我已经好几天了。
从CSV文件中添加示例表。
不完全遵循你所要问的是什么?你可以尝试在你的问题上更具体吗?您的问题是否自动输入外键? – mahatmanich
我认为一个数据样本应该就足够了! – mahatmanich
对不起,这是我的第一篇文章。掌握这个评论部分。 所以我有一个包含以下几列CSV文件较大: -name(机场名) -port_code(机场代码例如:JFK,LAX) - shipment_type(空运或海运),目前相关的Shipment_Type表 -country_code(例如:美国,德国,中国)目前与国家表相关联。 – vinicomelo