16

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>'
tschale
  • 975
  • 1
  • 18
  • 34
  • 1
    Can you write the error backtrace? – mdesantis May 09 '14 at 15:13
  • You might have to use `reload!` in your console to make it re-read all the files of your app, and make it aware of all the changes that were made. – MrYoshiji May 09 '14 at 15:13
  • Try like this `a= Article.first` and then `a.tags.create(name: "tag")`. – Pavan May 09 '14 at 15:19
  • `reloading` has no effect. I also tried exiting and restarting the rails console to no avail – tschale May 09 '14 at 15:21
  • @Pavan: this gets the same result... – tschale May 09 '14 at 15:21
  • Yes, same error and same error backtrace – tschale May 09 '14 at 15:29
  • @mdesantis: added error backtrace – tschale May 09 '14 at 15:44
  • Sounds weird... did you run the migration (using `rake db:migrate`)? – mdesantis May 09 '14 at 15:46
  • And: do you have a counter somewhere in your models? It seems somewhat related – mdesantis May 09 '14 at 15:47
  • @mdesantis: yes, I did run the migration. What do you mean by counter? – tschale May 09 '14 at 15:58
  • I mean an [has_many :counter_cache](http://docs.rubydocs.org/ruby-2-1-2-rails-4-1-1/Rails%204.1.1/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many) or similar, it seems related looking at the backtrace – mdesantis May 09 '14 at 16:06
  • You're right, it somehow seems related, but adding it (and adding the according column) didn't change anything. Same error, exact same backtrace... – tschale May 09 '14 at 17:16
  • I'm giving up for now. I basically did the tutorial anew and left out any for this functionality uneeded part, but it has still the same error. I also tried some of the options mentioned in the rubydocs to no effect. Maybe some part of my rails installation/configuration is messed up, so maybe I happen to run again into this problem and hopefully then find a solution which I can post here... But thanks for your help anyway! – tschale May 09 '14 at 18:29
  • @mdesantis: as posted in my answer, I got it to work by switching ruby versions... – tschale May 11 '14 at 09:24

4 Answers4

15

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.

tschale
  • 975
  • 1
  • 18
  • 34
12

Upgrade to Rails 4.0.13 (or later) or downgrading to Ruby 2.1.1 solves the problem.

Nils Ivanson
  • 121
  • 1
  • 2
  • 1
    Ruby `2.1.8` solves the problem as well (which I needed because of the supported ruby versions on Heroku) , but nevertheless, your answer has directed me in the right direction. Thanks – Aleks Mar 13 '16 at 15:29
  • downgrading from Rails `4.0.2` to `4.0.13` works. I was using activerecord `4.0.2` and I downgraded to `4.0.13`. Also my ruby version is `2.3.3p222` – Fabrizio Bertoglio Nov 13 '17 at 17:26
5

FYI, I can confirm this is an issue with ActiveRecord and Ruby 2.2. I was using ActiveRecord 3.2 and since changing to the 3-2-stable branch, the issue is gone. I believe this is fixed now in 4.x branches. I have raised an issue for this at https://github.com/rails/rails/issues/18991.

Matthew O'Riordan
  • 7,981
  • 4
  • 45
  • 59
  • I can double-confirm this. I was trying to use **Rails 3.2.21** with **Ruby 2.2.0** and no worky. Switched to the **Rails 3-2-stable** branch and it works great. – Joshua Pinter Feb 27 '15 at 04:19
0

I think there is a bug in activerecord 4.1.0 with has_cached_counter? and ruby 2.2. It was fixed on activerecord 4.1.2.

I got it to work by switching rails versions from 4.1.0 to 4.1.2.

Suneel
  • 371
  • 4
  • 14