添加到现有项目的seeds.rb,验证错误和未定义的方法

时间:2014-08-01 17:38:43

标签: ruby-on-rails ruby postgresql

我的任务是将种子添加到现有的Rails项目中。我已经获得了十二个调查名称的列表,我应该创建它们,存储它们的id(在调查列表中的位置)和名称值,然后循环它们并为每个调查名称添加占位符survey_question。如果我要创建一个新的迁移,重新定义survey_question作为调查的属性,我知道该怎么做,但我应该使用现有的结构,其中survey和survey_question是不同的类。而且我撞墙了。

我的想法是将所有调查名称放在一个数组中,因此在数据库播种时自动排序:

surveys = [
  "SurveyA",
  "SurveyB",
  "SurveyC",
  "SurveyD",
  "SurveyE",
  "SurveyF",
  "SurveyG",
  "SurveyH",
  "SurveyI",
  "SurveyJ",
  "SurveyK",
  "SurveyL"
]

然后编写一个实例化Survey类的每个循环,并将给定的名称存储为数据库条目的name属性。在同一个循环中,我将survey_question定义为占位符问题(你们中的一些人可能认识到),并尝试将其附加到使用<<<<<运营商。这是我的第一个失败点:

surveys.each do |survey|
  survey_question = SurveyQuestion.create(body: "What is the average flight speed velocity of an unladen swallow?")
  Survey.create(name: survey) << survey_question
end

太好了,我写了一个循环。我需要验证它是否符合我的要求。所以我去重新种子数据库并运行rake db:seed。但是,当我这样做时,我从项目附带的seeds.rb文件中的一行中得到验证错误。见这里:

rake aborted!
undefined method `<<' for #<Survey:0x007fb01fe1a3d0>
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activemodel-4.0.3/lib/active_model/attribute_methods.rb:439:in `method_missing'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/attribute_methods.rb:155:in `method_missing'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:95:in `block in <top (required)>'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `each'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `<top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.3/lib/rails/engine.rb:540:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:140:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed

以下是来自种子的违规行(25):

  #create seed region
  usa = Region.create!(name: "USA")

所以我想,也许是因为一些奇怪的原因,这是在创建数据库时写的,并且无法覆盖,所以如果我清理数据库并重新开始,它将填充而不会抱怨。我跑了rake db:reset。错误。在这一点上,我意识到我编写上面每个循环的方式有错误。错误消息告诉我&lt;&lt;是附加到的调查对象的未定义方法。这就是麻烦。

rake aborted!
undefined method `<<' for #<Survey:0x007fb01fe1a3d0>
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activemodel-4.0.3/lib/active_model/attribute_methods.rb:439:in `method_missing'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/attribute_methods.rb:155:in `method_missing'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:95:in `block in <top (required)>'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `each'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `<top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.3/lib/rails/engine.rb:540:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:140:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed

作为数据的最后一点,我查看了数据库中实际存在的内容,只是为了看看那里有什么。这是SELECT * FROM "regions";

的结果
erp_development=# SELECT * FROM "regions"
erp_development-# ;
 id | name |         created_at         |         updated_at         | default_email_sig 
----+------+----------------------------+----------------------------+-------------------
  1 | USA  | 2014-08-01 17:13:06.733146 | 2014-08-01 17:13:06.733146 | 
(1 row)

所以现在我很好并且坚持了下来。我一直在学习,但我对Ruby和Ruby on Rails的理解充其量只是入门级的。如果Stack-land的任何人都可以帮助我解决这个问题,那么我将永远为你负债。

谢谢, 布赖恩

2 个答案:

答案 0 :(得分:2)

&LT;&LT;是Ruby Array方法。 Survey.create(name:survey)不会返回你的数组,所以这就是它失败的原因。 根据文档.create方法返回结果对象! http://apidock.com/rails/ActiveRecord/Base/create/class

survey = Survey.create(name: survey)
survey.survey_questions << question

上面的代码应该可以使用!

答案 1 :(得分:0)

<<方法只修改survey.survey_questions的返回值,它不会修改对象本身。您应该使用=+=等修改方法。无论如何,我认为这是一种更清洁的方式:

surveys.map do |survey|
  Survey.create! do |s|
    s.name = survey
    s.survey_questions.build(body: "What is the average flight speed velocity of an unladen swallow?")
  end
end