Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveRecord relationships - has many and belongs to associations

I've created 3 models:

  • Article: contains an article
  • Tag: contains tags
  • ArticleTag: meant for associating a many-to-one tags to article relationship. It contains a tag_id and an article_id.

The problem I'm having is I'm fairly new to the active record technology and I don't understand the proper way to define everything. Currently, which I think is wrong, is I have a

ArticleTag
 belongs_to :article
 belongs_to :tag

Now, from here my thought was to then add

  Article
   :has_many :tag

I'm not sure if im approaching this correctly at all. Thanks for the help!

like image 430
somejkuser Avatar asked Dec 07 '22 05:12

somejkuser


2 Answers

It depends whether you want a join model or not. A join model lets you hold extra information against the association between two other models. For example, perhaps you want to record a timestamp of when the article was tagged. That information would be recorded against the join model.

If you don't want a join model, then you could use a simple has_and_belongs_to_many association:

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :articles  
end

With a Tagging join model (which is a better name than ArticleTag), it would look like this:

class Article < ActiveRecord::Base
  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 :article
  belongs_to :tag
end
  • A Guide to Active Record Associations
like image 159
John Topley Avatar answered Dec 28 '22 06:12

John Topley


You should use has_many when the relationship is one-way. An article has many tags, but tags also have many articles, so that's not quite right. A better choice might be has_and_belongs_to_many: an Article has many Tags, and those Tags also reference articles. A belongs_to B means that A references B; A has_one B means that B references A.

Here's a summary of the relationships you might see:

Article
  has_and_belongs_to_many :tags   # An article has many tags, and those tags are on
                                  #  many articles.

  has_one                 :author # An article has one author.

  has_many                :images # Images only belong to one article.

  belongs_to              :blog   # This article belongs to one blog. (We don't know
                                  #  just from looking at this if a blog also has
                                  #  one article or many.)
like image 45
John Feminella Avatar answered Dec 28 '22 06:12

John Feminella