Zachary B
Zachary B

Reputation: 1

ActiveRecord::UnknownAttributeError: unknown attribute 'article_id' for Tagging

I am work on a Blogger Rails App from section I3 :

http://tutorials.jumpstartlab.com/projects/blogger.html#blogger-2

I am getting an error when running the console and attempting to run:

a.tags.create name: "tag1"

After I run:

a = Article.first

tagging.rb file:

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

article.rb file:

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

tag.rb file:

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

I tried adding has_many :article_id to the tagging class, raked the db:migration and ran it again and it came back with a no method for nill class error.

Where should I define the article_id? Does it need to go in to migration file for CreateTagging?

Upvotes: 0

Views: 2230

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

The tagging.rb file is your join file, and you have article_id and tag_id defined in there.

I note that you have belongs_to :articles ... you should be using the singular ... belongs_to :article. That's likely why you're failing. Provided you did...

generate model Tagging tag:references article:references

... as the tutorial suggests, that would have automatically created the two id fields for you in the taggings table.

You can confirm that by examining the db/schema.rb

Upvotes: 1

Related Questions