Forum: Ruby on Rails HABTM on 3 tables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Panda B. (Guest)
on 2008-10-24 18:16
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
Panda B. (Guest)
on 2008-10-24 18:23
just an information to be more clear:
I had yet created a table called "articles_tags_users" in the database.
Jochen K. (Guest)
on 2008-10-24 18:40
(Received via mailing list)
Am 24.10.2008 um 16:23 schrieb Panda B.:

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

--
Jochen
Mark Reginald J. (Guest)
on 2008-10-24 19:27
(Received via mailing list)
Panda B. wrote:
>
> 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.

--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
Panda B. (Guest)
on 2008-10-24 21:03
Jochen K. wrote:
> I think you are looking for something like this:
> http://agilewebdevelopment.com/plugins/acts_as_tag...

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 J. 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.
Panda B. (Guest)
on 2008-10-25 13:02
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!
Felipe V. (Guest)
on 2008-12-19 02:53
(Received via mailing list)
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



On Sat, Oct 25, 2008 at 6:02 AM, Panda B. <
removed_email_address@domain.invalid> wrote:

>  has_and_belongs_to_many :users, :join_table => :articles_tags_users
>
> >
>


--
Felipe V. Contesse
Ingeniería Civil Industrial UC
This topic is locked and can not be replied to.