ruby-on-rails-3 - ActiveRecord::Base#create - 类型错误:无法将符号转换为整数

标签 ruby-on-rails-3 activerecord

我有

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 中,我输入

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 想要建立成员和帖子的关系,但它不应该只是让这些为零。我什至尝试在 seeds.rb 中显式地将数组设置为 nil,但得到了相同的错误。

我不明白什么?谢谢!

更新:添加了帖子和成员(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 出现了问题。但你可以用不同的方式编写来解决这个问题。

关于ruby-on-rails-3 - ActiveRecord::Base#create - 类型错误:无法将符号转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14245919/

相关文章:

ruby-on-rails - 更新到 Rails 3.2.2 : How to properly move my plugin from the '/vendor' to '/lib' directory?

ruby-on-rails - 有没有人想出如何在 Rails 3.2 中使用 Redcarpet gem for Markdown 嵌入视频?

php - 寻求有关在 CodeIgniter 中加入查询的帮助

ruby-on-rails - 如何销毁has_many关联加join表记录?

ruby-on-rails - Rails 获取上一个和下一个事件记录对象的最佳方法

ruby-on-rails - Rails 3 应用程序中未初始化的常量 UsersController::PostComment

ruby-on-rails - 编译错误,意外的 kENSURE,预计 Rails 3 项目中出现 $end 错误

ruby-on-rails-3 - 如何在运行时更改ActiveRecord的表名

ruby-on-rails - 当涉及 pluck 时 to_sql 不起作用

ruby-on-rails - Rails 5 Active Record - 是否可以在内存中保存一个表?