我正在解析excel并保存员工并将原始数据投影到规范化表中。我有2个型号
Excel Like
employee.rb
class Employee < ActiveRecord::Base
attr_accesible :name. :salary, :band
has_and_belongs_to_many :projects
end
project.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
请建议一些更好的方法,将项目关联与员工保存在一行