Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

ruby on rails - create with has_many through association gets NoMethodError (undefined method `name' for nil:NilClass)

I'm doing this tutorial and am stuck in the Tagging part. Basically I have articles that can have a list of tags. As one article can has multiple tags and vice versa, there is an additional taggings model, through which this association is modelled. Here are the models:

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

and the migrations:

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

There's also an article_controller with (amongst others):

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

    redirect_to article_path(@article)
end

Now, as the tutorial suggets, when I try to create a new tag with the rails console for an article, I get a NoMethodError for a 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

So it seems to me, as if the tag entry is created, as well as the correct taggings entry, but apparently at some point it struggles to find the correct tag, hence the error. Am I right? How can I fix this?

I found a lot of questions on SO regarding this kind of error, but each was caused by some problem I couldn't relate to mine...

UPDATE:

reloading or restarting the rails console has no effect.

Here is the error backtrace, with path as ~/.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>'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got it to work by switching ruby versions. I was using ruby-head, which is ruby 2.2.0dev. Switched back to ruby-2.1.1, now it works without errors. Should've probably tried that earlier...

Maybe this could help others facing similar errors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...