【发布时间】:2015-04-08 10:17:44
【问题描述】:
表_1:
ID | Name | Address
1 | prakash | 2-107, NY
2 | rakesh | 2-109/a, NY
表_2:
emp_ID| Name | Designation
? | Prakash | Software Developer
? | Rakesh | Software Tester
通过使用 table-2 中的 Name 列,我需要从 table-1 中获取 id 并将其放入 table-2 中。
require 'csv'
load 'dbconnection.rb'
require 'activerecord'
class Employee<ActiveRecord::Base
end
class Designation<ActiveRecord::Base
end
#Employee Table Data Insertion
CSV.foreach("C:/Users/modi/Desktop/employee.csv") do |row|
Employee.create!(
# ID will be auto increement
:name => row[0],
:address => row[1],
)
end
#Designation Table Data Insertion
CSV.foreach('C:/Users/modi/Desktop/designation.csv') do |row|
Designation.create!(
:Emp_id #Here we need to get the id value from the Employee Table
:name => row[0],
:designation=>row[1]
)
end
【问题讨论】:
-
你的 csv 文件有标题吗?
-
不,我没有使用@shivam
-
澄清一下,这两个
CSV.foreach是同一个文件的一部分吗?
标签: mysql ruby csv activerecord rubygems