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.
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
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