2016-11-28 57 views
0

我想我打破了我的项目。 :(我做了很多改变,其中之一是改变我的资源,所以它们嵌套(浅:真)。我试图种子我的数据库再次,我得到一个TypeError:没有隐式转换符号到整数消息。我已经通过计算器望去,一个人也有类似的问题,但没有答案,我还没有发现,在互联网上工作的其他地方的答案嵌套数据库与嵌套资源:没有将符号隐式转换为整数

我的路线文件:

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=> '[email protected]', 
    :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 

让我知道是否有任何可能需要的其他信息。我会继续拉我的头发,盯着代码。 :)

回答

0

看到这个答案:

Rails3 seed data nested attribute

这里是关键部分:

If you are wanting to try and use the nested attributes way, then you don't need the :vendor => {} part of the hash, you can go straight into the params, and you need addresses_attributes to be an array, not a hash.