Do I need to manually create a migration for a HABTM join table?
Asked Answered
E

4

77

I'm struggling now to get HATBM working correctly. I have a beaten scanario: articles and tags. I presume, HABTM should be used here, since it is a many-to-many relationship. I don't know however if I should manually create a join table (articles_tags in this case).

My code currently as follows:

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags  
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :articles
end

When I run the migrations, no 3rd table is created. Also, I would like to add that my third table doesn't bear any domain logic, just blind assignment.

I'm using Rails 2.2.2

Extension answered 19/2, 2009 at 7:41 Comment(2)
Even at rails 4, I'm guessing the answer to this is .... "yes"? :(Hominy
@dtc, still yes, still true.Extension
I
140

You should do this in a migration of one of the tables, or in a separate migration if those migrations have been ran:

create_table :articles_tags, :id => false do |t|
  t.references :article, :tag
end

add_index :articles_tags, [:article_id, :tag_id]

This will create the table for you and the :id => false tells Rails not to add an id field to this table. There's an index also, which will speed up lookups for this join table.

You could also generate a model (ArticlesTag) for this and do:

# article.rb
has_many :articles_tags
has_many :tags, :through => :articles_tags

# tag.rb
has_many :articles_tags
has_many :articles, :through => :articles_tags

# article_tag.rb
belongs_to :tag
belongs_to :article

And then create the table in the migration generated from the script/generate model articles_tag call.

Illustrator answered 19/2, 2009 at 7:50 Comment(5)
Funny that official guides or documentation does not state this. I had to dig on Stackoverflow. Ty.Neurology
What if you wanted to relate them on with a unique name for the join because that join was already being used.Paske
I think you should add indexes to join table by adding add_index :articles_tags, [:article_id, :tag_id] before the end of change methodSemeiology
Should it not be :articles_tag (singular) in the model?Mauricio
I've added create_table :articles_tags... to my Tag migration. What I should do so my rake db:rollback works correctly?Baluster
D
8

Note that this is covered in the API.

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

Dirkdirks answered 8/3, 2011 at 12:39 Comment(2)
Though it ought to be mentioned in a guide someplace.Ruthieruthless
Read these few lines in the API so you understand the expected name of the table (e.g. when using the same prefix). This will explain the comment of @Giovanni .Ulcerate
R
7

You probably also want to add an index to the migration:

add_index "articles_tags", "article_id"

add_index "articles_tags", "tag_id"

However, if you want tagging functionality I'd recommend the acts_as_taggable_on rails plugin:

http://www.intridea.com/tag/acts_as_taggable_on http://github.com/mbleigh/acts-as-taggable-on/

I've used it on a project and it was very easy to implement.

One of the issues with a join table for tagging is that it can easily get ugly creating a join table for each content type you wish to make taggable (ie. comments_tags, posts_tags, images_tags, etc). This plugin uses a taggings table which includes a discriminator to determine the content type without the need of a specific join table for each type.

Ruscher answered 26/2, 2009 at 5:26 Comment(3)
Migration methods references (or belongs_to) automatically adds indexes. You do not need that. You can break your migration with it.Neurology
Migration methods references (or belongs_to) really do not automatically adds indexes. (Thought they do :-)Neurology
IMHO it would be better to create a composite primary key. But it's database specific.Gautious
R
1

In combination with this Qeuestion(1st answear) How to set up a typical users HABTM roles relationship and 1st answear from here, it has to be understood even by a monkey. I am new in RoR and it's got working like a charm

Revisionist answered 19/9, 2012 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.