在我的控制台中测试两个表之间的关联时,我遇到了上述问题。
这就是我的模型的样子:
class Team < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :plan
has_and_belongs_to_many :teams
end
以下是我生成的用于连接的迁移引用上表:
class CreateTeamsUsers < ActiveRecord::Migration
def change
create_table :teams_users, :id => false do |t|
t.integer :team_id
t.integer :user_id
end
add_index :teams_users, :team_id
add_index :teams_users, :user_id
end
end
当我尝试将控制台用户推送到团队时,反之亦然,我收到以下错误:
NoMethodError: undefined method `name' for nil:NilClass
知道什么可以产生这样的问题?
谢谢!
更新
这是我的schema.rb
# encoding: UTF-8
ActiveRecord::Schema.define(version: 20160207132759) do
create_table "roles", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "teams", force: true do |t|
t.string "name"
t.integer "value"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "teams_users", id: false, force: true do |t|
t.integer "team_id"
t.integer "user_id"
end
add_index "teams_users", ["team_id"], name: "index_teams_users_on_team_id"
add_index "teams_users", ["user_id"], name: "index_teams_users_on_user_id"
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "role_id"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
以下是我在控制台中运行的代码:
u = User.new
u.email = "test@test.com"
u.password = "123456789"
u.save
t = Team.new
t.name = "test teat"
t.save
u.teams << t
根据要求;这是错误本身。我还发现我在尝试destroy_all用户时遇到了同样的错误。
看起来错误发生在“联接表”
中NoMethodError: undefined method `name' for nil:NilClass
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/has_many_association.rb:79:in `cached_counter_attribute_name'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/has_many_association.rb:75:in `has_cached_counter?'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/has_many_association.rb:83:in `update_counter'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/has_many_through_association.rb:65:in `insert_record'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/collection_association.rb:522:in `block (2 levels) in concat_records'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/collection_association.rb:389:in `add_to_target'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/collection_association.rb:521:in `block in concat_records'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/collection_association.rb:519:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/collection_association.rb:519:in `concat_records'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/has_many_through_association.rb:42:in `concat_records'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/collection_association.rb:153:in `block in concat'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/associations/collection_association.rb:168:in `block in transaction'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.1.0/lib/active_record/transactions.rb:208:in `transaction'
... 6 levels...
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `block in require'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
from /home/ubuntu/workspace/criteofootball/bin/rails:9:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
答案 0 :(得分:0)
我认为与Ruby 2.2.0一起使用时,这是ActiveRecord 4.1.0中的一个错误。您可以看到它影响另一个项目here的示例。修复程序在ActiveRecord 4.1.2中,因此升级应该为您解决此问题。