我尝试rake db:seed
迁移以通过种子助手导入数据。
一个rake工作(01_roles),但另一个rake(02_conditions)表示存在mass-assign protected属性。
夹具/开发/ 01_roles.rb
SeedHelper.make_roles
夹具/开发/ 02_conditions.rb
SeedHelper.make_conditions
seed_Helper.rb
class SeedHelper
include Singleton
def self.make_roles
file_path = "#{Rails.root}/db/roles.csv"
RoleImporter::import(file_path)
end
def self.make_conditions
file_path = "#{Rails.root}/db/conditions.csv"
ConditionImporter::import(file_path)
end
end
模型/ condition.rb
class Condition < ActiveRecord::Base
attr_accessible :condition, :bodypart, :description
end
我感觉它正在调用第一个fixture(01_roles),因为该文件中有一个“description”变量,并且它不会将其称为错误:
Can't mass-assign protected attributes: condition, bodypart
模型/ role.rb
class Role < ActiveRecord::Base
attr_accessible :description, :profession, :tag_list, :name
我在哪里更改rake文件或导入程序以读取正确的文件?
答案 0 :(得分:0)
找到答案:
我需要将require 'condition_importer'
添加到importer.rb
然后在condition_importer.rb中,我通过复制文件Role.create!
而不是Condition.create!
:
class ConditionImporter
def self.import(file_path)
CSV.readlines(file_path, {:headers => :first_row, :encoding => 'UTF-8'}).each do |row|
Condition.create!(
condition: row['Condition'],
bodypart: row['Bodypart'],
description: row['Description'])
end
end
end