[英]Rails5: How to set a variable for seed params
我正在为具有许多相似列的不同表播种多个csv。 例如,教师和学生表具有相似的列电子邮件,名称等。因此,我想通过将所有常见内容放入变量中来缩短种子文件,以便:
puts "Seeding teachers.."
File.open("teachers.csv", "r") do |f|
f.each_with_index do |line, index|
email, name, teacher_code = line.chomp.split (",")
Teacher.create(email: email, name: name, teacher_code: teacher_code)
end
end
puts "Seeding students.."
File.open("students.csv", "r") do |f|
f.each_with_index do |line, index|
email, name, subject = line.chomp.split (",")
Student.create(email: email, name: name, subject: subject)
end
end
变成:
def common_data
[email, mame]
end
def common_params
["email: email", "name: name"] # not sure
end
puts "Seeding teachers.."
File.open("teachers.csv", "r") do |f|
f.each_with_index do |line, index|
common_data, teacher_code = line.chomp.split (",")
Teacher.create(common_params, teacher_code: teacher_code)
end
end
puts "Seeding students.."
File.open("students.csv", "r") do |f|
f.each_with_index do |line, index|
common_data, subject = line.chomp.split (",")
Student.create(common_params, subject: subject)
end
end
我得到一个错误
NameError: undefined local variable or method `email' for main:Object
错误被抛出common_data
,是吗? 因为,这里:
[email, mame]
(假设“ mame”应该是“ name”),ruby希望email
是一个变量或方法。 但是不是吗?
也许您的意思是:
['email', 'name']
要么
[:email, :name]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.