2013-01-09 45 views
2

的ActiveRecord :: Base的#創建 - 類型錯誤:不能轉換成符號整數

class CreateRoles < ActiveRecord::Migration 
    def change 
    create_table :roles do |t| 
     t.string :name 

     t.timestamps 
    end 
    end 
end 

class Role < ActiveRecord::Base 
    attr_accessible :name 
    has_many :members, :posts 
end 

class Post < ActiveRecord::Base 
    attr_accessible :content, :title, :role_id 
    belongs_to :role 
end 

class Member < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :lockable 

    attr_accessible :role_id, :first_name, :last_name, :email, :password, :password_confirmation, :remember_me 
end 

在Rails的控制檯或seeds.rb,I型

Role.create(name: 'guest') 

並且得到錯誤

TypeError: can't convert Symbol into Integer 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:35:in `[]' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:35:in `wrap_block_extension' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:22:in `build' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/autosave_association.rb:139:in `build' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/has_many.rb:10:in `build' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:13:in `build' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations.rb:1195:in `has_many' 
    from /Users/ataylo9/Dropbox/Developer/hamsterdam/app/models/role.rb:3:in `<class:Role>' 
    from /Users/ataylo9/Dropbox/Developer/hamsterdam/app/models/role.rb:1:in `<top (required)>' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `block in load_file' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:639:in `new_constants_in' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:468:in `load_file' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:353:in `require_or_load' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:502:in `load_missing_constant' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in `block in const_missing' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `each' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `const_missing' 
    from (irb):1 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start' 
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>' 
    from script/rails:6:in `require' 

我知道我得到的錯誤,因爲Rails想建立成員和帖子的關係,但不應該只是使這些零。我甚至試着明確地將數組設置爲seed.rb中的nil,但得到了同樣的錯誤。

我什麼都不明白?謝謝!

更新:新增崗位和會員模型參考

回答

0

看起來你分配關係(的has_many:成員:職位),它不存在。你有這樣的模型嗎?模型有這種關係嗎?你可以發佈會員和郵政模式的代碼嗎?

+1

我在問題中添加了它們。謝謝! – hamsterdam

+0

我遇到了在單行中聲明多個'has_many'關係的問題。請參閱下面的@jizak參考答案,它幫助我識別此問題 –

3

我使用相同的模型創建了相同的項目。而我發現這種描述關係發生那種錯誤。

class Role < ActiveRecord::Base 
    attr_accessible :name 
    has_many :members, :posts 
end 

我嘗試這樣做:

​​

和一切正常。我不知道爲什麼,但看起來像has_many:帖子,:成員發生的問題。但是你可以用不同的方式來解決這個問題。

2

jizak的答案是什麼幫助推動我在正確的方向,找到解決方案,以我自己的問題。起初,我試圖添加多個「項目」到一個單一的has_many,如上面的例子:

has_many :members, :posts

我的Rails控制檯遊戲我同樣的錯誤 - 「類型錯誤:無法轉換成符號整數。 「所以我把它分成兩條不同的路線:

has_many :members
has_many :posts

現在它工作正常。我猜想我試圖有點太聰明,思考關聯(has_many)會類似於attr_accessor/writer/reader - 可以在單個attr_x(或在這種情況下,has_many)中添加多個東西。 Rails並非如此(據我所知) - 每個關聯都需要自己的單獨聲明。

+0

這有助於我的'TypeError:無法將符號轉換爲整數'。謝謝! – Legat

相關問題