我正在解析 excel 并将员工和项目原始数据保存到规范化表中。我有 2 个模型
Excel Like
员工.rb
class Employee < ActiveRecord::Base
attr_accesible :name. :salary, :band
has_and_belongs_to_many :projects
end
项目.rb
class Project< ActiveRecord::Base
attr_accesible :name
has_and_belongs_to_many :employees
end
创建了一个连接表
employees_projects
现在我正在做一些接近
Employee.transaction do
1000.times do |i|
emp = Employee.new(:name => 'Employee#{i}',
:salary => i * 1000,
:band => 'A')
emp.projects.push(Project.find(i)) #the projects are already populated bfr this activity
emp.save!
end
end
请提出一些更好的方法来在一行中保存与员工的项目关联