【问题标题】:Seeding DB with nested resources: no implicit conversion of symbol into integer使用嵌套资源播种 DB:没有将符号隐式转换为整数
【发布时间】:2017-04-12 11:40:30
【问题描述】:

我想我破坏了我的项目。 :( 我做了很多改变,其中之一是改变我的资源,所以它们是嵌套的(浅:真)。我试图再次为我的数据库播种,我得到一个 TypeError:没有将 Symbol 隐式转换为 Integer 消息。我查看了 StackOverFlow,有人遇到类似问题但没有答案;我还没有找到适用于互联网其他地方的答案。

我的路线文件:

Goalie::Application.routes.draw do


  resources :users, shallow: true do
    resources :goals
    resources :steps
    resources :habits
  end

我的架构如下所示:

ActiveRecord::Schema.define(:version => 20161121101351) do

  create_table "users", :force => true do |t|
    t.integer  "user_id"
    t.string   "firstname"
    t.string   "lastname"
    t.string   "username"
    t.string   "password"
    t.string   "userEmail"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end


  create_table "goals", :force => true do |t|
    t.integer  "goal_id"
    t.string   "name"
    t.string   "description"
    t.date     "deadline"
    t.boolean  "completed"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "steps", :force => true do |t|
    t.integer  "step_id"
    t.string   "name"
    t.string   "description"
    t.boolean  "completed"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end


  create_table "habits", :force => true do |t|
    t.integer  "habit_id"
    t.string   "name"
    t.string   "description"
    t.boolean  "frequency"
    t.integer  "frequencytime"
    t.datetime "created_at",    :null => false
    t.datetime "updated_at",    :null => false
  end
end

seed.rb 文件如下所示:

params =  { :user =>
    {
    :user_id => '1',
    :firstname => 'Salaman',
    :lastname=> 'Rushdie',
    :username=> 'bigboss',
    :password=> 'admin',
    :userEmail=> 'bigboss@hotmail.com',
    :goals_attributes => {
        :goal_id => '1',
        :name => 'Lose weight',
        :description => 'Lose 30lb',
        :deadline => '25/12/2016',
        :completed => false,
        :steps_attributes => {
            :step_id => '1',
            :name => 'Work out',
            :description => 'Exercise to achieve weight loss goal',
            :completed => false,
              :habits_attributes => {
              :habit_id => '1',
              :name => 'Gym',
              :description => 'Go to the gym 5 times a week',
              :frequency => 'true',
              :frequencytime => '5',
              }
          }
      }
  }
}
User.create!(params[:user])

当我尝试为数据库播种时,我收到以下消息:

$ rake db:seed --trace
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
rake aborted!
TypeError: no implicit conversion of Symbol into Integer
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/nested_attributes.rb:396:in `[]'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/nested_attributes.rb:396:in `block in assign_nested_attributes_for_col
lection_association'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/nested_attributes.rb:396:in `map'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/nested_attributes.rb:396:in `assign_nested_attributes_for_collection_a
ssociation'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/nested_attributes.rb:288:in `goals_attributes='
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/attribute_assignment.rb:94:in `block in assign_attributes'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/attribute_assignment.rb:93:in `each'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/attribute_assignment.rb:93:in `assign_attributes'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/base.rb:498:in `initialize'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/persistence.rb:44:in `new'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/persistence.rb:44:in `create'
c:/users/cat/Goalie/db/seeds.rb:39:in `<top (required)>'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-3.2.8/lib/act
ive_support/dependencies.rb:245:in `load'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-3.2.8/lib/act
ive_support/dependencies.rb:245:in `block in load'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-3.2.8/lib/act
ive_support/dependencies.rb:236:in `load_dependency'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-3.2.8/lib/act
ive_support/dependencies.rb:245:in `load'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-3.2.8/lib/rails/en
gine.rb:520:in `load_seed'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-3.2.8/lib/acti
ve_record/railties/databases.rake:309:in `block (2 levels) in <top (required)>'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.r
b:248:in `call'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.r
b:248:in `block in execute'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.r
b:243:in `each'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.r
b:243:in `execute'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.r
b:187:in `block in invoke_with_call_chain'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.r
b:180:in `invoke_with_call_chain'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.r
b:173:in `invoke'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:152:in `invoke_task'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:108:in `block (2 levels) in top_level'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:108:in `each'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:108:in `block in top_level'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:117:in `run_with_threads'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:102:in `top_level'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:80:in `block in run'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:178:in `standard_exception_handling'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/applic
ation.rb:77:in `run'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-11.3.0/exe/rake:27:in
`<top (required)>'
c:/RailsInstaller/Ruby2.1.0/bin/rake:23:in `load'
c:/RailsInstaller/Ruby2.1.0/bin/rake:23:in `<main>'
Tasks: TOP => db:seed

如果您还需要任何其他信息,请告诉我。我会继续拔头发,盯着代码看。 :)

【问题讨论】:

    标签: ruby-on-rails database integer symbols seed


    【解决方案1】:

    看到这个答案:

    Rails3 seed data nested attribute

    这里是关键部分:

    如果你想尝试使用嵌套属性的方式,那么你不需要哈希的 :vendor => {} 部分,你可以直接进入参数,你需要addresses_attributes是一个数组,而不是哈希。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多