轨道上的红宝石 3 - 活动记录::基#创建 - 类型错误:无法将符号转换为整数



我有

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控制台或种子中。b,输入

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想要建立成员和帖子的关系,但它不应该只是使那些nil。我甚至尝试在种子中显式地将数组设置为nil。

我不明白什么?谢谢!

更新:添加Post和Member模型供参考

我用相同的模型创建了相同的项目。而我发现这种描述关系出现了那个错误。

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

我试过了:

class Role < ActiveRecord::Base
  attr_accessible :name
  has_many :members
  has_many :posts
end
class Post < ActiveRecord::Base
  attr_accessible :name, :role_id
  belongs_to :role
end
class Member < ActiveRecord::Base
  attr_accessible :name, :role_id
  belongs_to :role
end

一切都正常。我不知道为什么,但看起来像has_many:posts,:members发生的问题。但是你可以用不同的方式来解决这个问题。

jizak的回答帮助我找到了正确的方向,找到了我自己问题的解决方案。起初,我试图将多个"项"添加到单个has_many中,如上面的示例所示:

has_many:members,:posts

我的Rails控制台游戏我同样的错误-"TypeError: can't convert Symbol into Integer."所以我把它分成两行:


has_many:成员has_many:文章

现在可以正常工作了。

我想我有点太聪明了,认为关联(has_many)将类似于attr_accessor/writer/reader -其中可以向单个attr_x(或在本例中为has_many)添加多个东西。这不是Rails的情况(据我所知)-每个关联都需要自己的单独声明。

看起来您分配的关系(has_many:members,:posts)并不存在。你们有这样的型号吗?模型有这样的关系吗?你可以邮寄成员的代码和邮寄模式吗?

相关内容

  • 没有找到相关文章