Rails HasAndBelongsToMany Association With Primary Key Error

When using has_and_belongs_to_many, I ran into this error, ActiveRecord::HasAndBelongsToManyAssociationWithPrimaryKeyError

Primary key is not allowed in a has_and_belongs_to_many join table (articles_users).

So check the linked-table , articles_users in my case, if there is a pk set by default.

Solution

Put the :id => false to the migration create_table SQL.

create_table :articles_users, :id => false do |t|
  t.integer :article_id
  t.integer :user_id
end
Qihuan Piao

Qihuan Piao

Originally from China and now living in Tokyo, I started this blog to practice English. Over the years, writing helped me find my own voice and explore thoughts about identity, life, and ordinary things that catch my curiosity. I mostly write for myself, but I love it when something here connects with someone else.

Meanwhile, what I wrote in Japanese

Multilingual Wordpress Plugin : qTranslate

June 21, 2010