使用关联“ id”之外的其他方式将CSV数据导入Rails应用。 [英] Importing CSV data into Rails app, using something other then the association "id"
问题描述
我正在尝试将一些CSV文件导入我的Rails应用程序。我学习并设法将表导入到模型中而没有关联。
I am trying to import a few CSV files into my rails app. I learned and managed to import tables into Models without association.
现在,我已经设法将数据导入到具有关联的表中,但是只能通过在CSV列中输入实际的 id数字来实现。尽管可以使用,但这并不是真正的选择,因为我有很多带有数千个ID的表。
Now i have managed to import the data into a table that has associations, but only by entering the actual "id" number on the CSV column. Although functional, this isn't really an option because i have many tables with thousands of IDs.
我的主要目标是能够使用CSV和键入实际值(存在于与其关联的其他模型中),而不是ID号。
My main goal is to be able to use the column in the CSV and type in the actual value (that exists in the other model it is associated with), instead of the id number.
我有一个Country模型和一个Ports模型。端口模型与country_id
I have a Country model and a Ports model. The Ports model is associated with 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
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
之所以起作用,是因为我能够手动添加它们,从而查看我创建的表单。
The associations are working because i am able to manually add them view my forms i create just fine.
<%= 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 %>
任何指导或帮助将不胜感激。我已经在圈子里呆了几天了。
Any guidance or help would be greatly appreciated. I have been going in circles with this for days now.
从CSV文件添加示例表。
ADDING SAMPLE TABLE FROM CSV FILE.
< a href = https://i.stack.imgur.com/4Vo10.png rel = nofollow noreferrer>
推荐答案
shipping_type是红宝石对象,您想发送一个字符串。
A shipment_type is a ruby object, you want to send a string.
如果您需要导入关系,请像这样在 Port
模型上添加方法
If you are needing to import relationships, add methods on the Port
model like so
class Port < ApplicationRecord
def shipment_type_name
shipment_type.try(:name)
end
def shipment_type_name=(name)
self.shipment_type = ShipmentType.where(:name => name).first_or_create
end
def country_country_code
country.try(:country_code)
end
def country_country_code=(code)
self.country = Country.where(:country_code => code).first
end
end
然后在CSV中发送 shipment_type_name
和 country_country_code
属性。
Then in the CSV you'd send a shipment_type_name
and country_country_code
attributes.
您将执行与其他关系类似的操作。
You would do something similar to other relationships.
这篇关于使用关联“ id”之外的其他方式将CSV数据导入Rails应用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!