Super Spread Sheet S³

Or little computing tricks and hacks

Rails: Create a HABTM association between categories and articles

While looking to migrate the legacy database from WordPress into my Rails app, I noticed that one of the specs for the database was wrong. An article IS allowed more than one category. Sigh! That part of the implementation was ready. After studying the different possibilities, I opted for having a join table. This is what I did:

rails g controller Categories create edit
rails g model Category name:string

and all what is needed in Rails for the new model.

Then I added the following to the newly created Category model:

class Category < ActiveRecord::Base
  has_and_belongs_to_many :articles

and to the preexisting Article model:

class Article < ActiveRecord::Base
  has_and_belongs_to_many :categories

Having this, I have to create the join table, whose migration is as follows:

class CreateCategoriesAndArticles < ActiveRecord::Migration
  def change
    create_table :categories_articles, id: false do |t|
      t.belongs_to :category, index: true
      t.belongs_to :article, index: true

Two things to note:

  1. The name of the table is crucial, otherwise the magic to access the categories of an article will not work.
  2. id: false tells the database manager to create a table without an indexable id.

If you create the table with the wrong name as I did, here is the migration to rename a table (taken from Stack overflow):

 class RenameOldTableToNewTable< ActiveRecord:Migration
   def change
     rename_table :old_table_name, :new_table_name

Once created, the output in

rails db

as described in as described here is:

development=# select column_name from information_schema.columns where table_name='categories_articles';
(2 rows)

To access the categories in an article, the magic I mentioned before, do:


which will return an array of the category ids associated with that article.

By the way, this is a HABTM association, Has And Belongs to Many. This association is done in the table pairing the two ids.

In this post I wrote about providing the option of selecting one or more categories for an article.


One response to “Rails: Create a HABTM association between categories and articles

  1. Pingback: Rails: Choosing and assigning in a HABTM association | Super Spread Sheet S³

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: