Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>'
like image 791
tschale Avatar asked May 09 '14 15:05

tschale


1 Answers

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.

like image 198
tschale Avatar answered Sep 18 '22 13:09

tschale