HABTM on 3 tables

Hi. Currently, I had 2 models linked with HABTM association: _Article_ and _Tag_

class Article < ActiveRecord::Base   has_and_belongs_to_many :tags end

class Tag < ActiveRecord::Base   has_and_belongs_to_many :articles end

But I believe I need to ad another model called _User_. Do you know how processed to get a HABTM association with: _Article_, _Tag_ and _User_?

Thx

just an information to be more clear: I had yet created a table called "articles_tags_users" in the database.

I think you are looking for something like this:

http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids

Panda Beer wrote:

Hi. Currently, I had 2 models linked with HABTM association: _Article_ and _Tag_

class Article < ActiveRecord::Base   has_and_belongs_to_many :tags end

class Tag < ActiveRecord::Base   has_and_belongs_to_many :articles end

But I believe I need to ad another model called _User_. Do you know how processed to get a HABTM association with: _Article_, _Tag_ and _User_?

Perhaps by creating a Hub model with three foreign keys, and using has_many through.

Jochen Kaechelin wrote:

I think you are looking for something like this: http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids

I think this solution is a little bit so specific and so complexe for my association need. I hope there is a more simple way include by defaut in ActiveRecord.

Mark Reginald James wrote:

Perhaps by creating a Hub model with three foreign keys, and using has_many through.

In my context, I prefer using only 3 models with a HABTM association rather than using 4 models with some has_many through.

Thanks for your ideas.

YES! I found the answer:

class Article < ActiveRecord::Base   has_and_belongs_to_many :tags, :join_table => :articles_tags_users   has_and_belongs_to_many :users, :join_table => :articles_tags_users end

class Tag < ActiveRecord::Base   has_and_belongs_to_many :articles, :join_table => :articles_tags_users   has_and_belongs_to_many :users, :join_table => :articles_tags_users end

class User < ActiveRecord::Base   has_and_belongs_to_many :articles, :join_table => :articles_tags_users   has_and_belongs_to_many :tags, :join_table => :articles_tags_users end

have fun!

Some help please!

How do you fill the table with the three foreign keys?? i put user = User.new tag = Tag.find(id) article = Article.find(id) user.tag << tag user.article << article

user.save

all this creates 2 tuples on the database articles_tags_users instead of one with all the values