使用has_many通过关联创建NoMethodError(未定义的方法`name' for nil:NilClass)

时间:2014-05-09 15:11:01

标签: ruby-on-rails ruby-on-rails-4

我正在做this tutorial并陷入了标记部分。基本上我有文章可以有一个标签列表。由于一篇文章可以有多个标签,反之亦然,因此还有一个额外的taggings模型,通过该模型建立此关联。以下是模型:

class Article < ActiveRecord::Base
    has_many :comments
    has_many :taggings
    has_many :tags, through: :taggings
end

class Tag < ActiveRecord::Base
    has_many :taggings
    has_many :articles, through: :taggings
end

class Tagging < ActiveRecord::Base
    belongs_to :tag
    belongs_to :article
end

和迁移:

def change
    create_table :articles do |t|
        t.string :title
        t.text :body
        t.timestamps
    end

    create_table :tags do |t|
        t.string :name
        t.timestamps
    end

    create_table :taggings do |t|
        t.references :tag, index: true
        t.references :article, index: true
        t.timestamps
    end

还有一个article_controller(其中包括):

def create
    @article = Article.new(article_params)
    @article.save

    redirect_to article_path(@article)
end

现在,正如教程所说,当我尝试使用rails控制台为文章创建新的tag时,NoMethodError获得了nil:NilClass

head :011 > Article.first.tags.create(name: "tag")
  Article Load (0.5ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT 1
  (0.2ms)  begin transaction
  SQL (0.8ms)  INSERT INTO "tags" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", ...], ["name", "tag"], ["updated_at", ...]]
  SQL (2.1ms)  INSERT INTO "taggings" ("article_id", "created_at", "tag_id", "updated_at") VALUES (?, ?, ?, ?)  [["article_id", 1], ["created_at", ...], ["tag_id", 7], ["updated_at", ...]]
 (0.6ms)  rollback transaction
NoMethodError: undefined method `name' for nil:NilClass

所以在我看来,好像创建了tag条目,以及正确的taggings条目,但显然在某些时候它很难找到正确的tag,因此错误。我对吗?我该如何解决这个问题?

关于这种错误,我发现了很多关于SO的问题,但是每个问题都是由于我无法与之相关的一些问题引起的......

更新:

reloading或重新启动rails控制台无效。

以下是错误回溯,路径~/.rvm/gems/ruby-head/gems/activerecord-4.0.4/lib/active_record

from path/associations/has_many_association.rb:81:in `cached_counter_attribute_name'
from path/associations/has_many_association.rb:77:in `has_cached_counter?'
from path/associations/has_many_association.rb:85:in `update_counter'
from path/associations/has_many_through_association.rb:66:in `insert_record'
from path/associations/collection_association.rb:463:in `block (2 levels) in create_record'
from path/associations/collection_association.rb:367:in `add_to_target'
from path/associations/collection_association.rb:461:in `block in create_record'
from path/associations/collection_association.rb:152:in `block in transaction'
from path/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
from path/connection_adapters/abstract/database_statements.rb:221:in `within_new_transaction'
from path/connection_adapters/abstract/database_statements.rb:213:in `transaction'
from path/transactions.rb:209:in `transaction'
from path/associations/collection_association.rb:151:in `transaction'
from path/associations/collection_association.rb:460:in `create_record'
from path/associations/collection_association.rb:121:in `create'
from path/associations/collection_proxy.rb:260:in `create'
from (irb):14
from ~/.rvm/gems/ruby-head/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from ~/.rvm/gems/ruby-head/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /.rvm/gems/ruby-head/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'

3 个答案:

答案 0 :(得分:15)

我通过切换ruby版本来实现它。我使用的是ruby-headruby 2.2.0dev。切换回ruby-2.1.1,现在它可以正常工作。应该早点尝试过......

也许这可以帮助其他人面临类似的错误。

答案 1 :(得分:12)

升级到Rails 4.0.13(或更高版本)或降级到Ruby 2.1.1解决了这个问题。

答案 2 :(得分:5)

仅供参考,我可以确认这是ActiveRecord和Ruby 2.2的问题。我正在使用ActiveRecord 3.2,因为改为3-2稳定分支,问题就消失了。我相信现在已经在4.x分支中修复了。我在https://github.com/rails/rails/issues/18991已经提出了一个问题。