Forum: Ruby on Rails HABTM 3 table relation

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.
916981dcb90882ae0f984dc33de0a592?d=identicon&s=25 Felipe Vergara (Guest)
on 2008-12-19 14:27
(Received via mailing list)
Some help please!!
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

then in the controller

user = User.new
tag = Tag.find(id)
article = Article.find(id)
user.tag << tag
user.article << article

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

Do somebody knows how to do this?

Thanks !
--
Felipe Vergara Contesse
Ingeniería Civil Industrial UC
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2008-12-20 06:52
(Received via mailing list)
On Dec 19, 8:26 am, "Felipe Vergara" <felverg...@gmail.com> wrote:
> Some help please!!
> class Article < ActiveRecord::Base
>  has_and_belongs_to_many :tags, :join_table => :articles_tags_users
[...]
> user = User.new
> tag = Tag.find(id)
> article = Article.find(id)
> user.tag << tag
> user.article << article
>
> all this creates 2 tuples on the database articles_tags_users instead of one
> with all the values
>
> Do somebody knows how to do this?

I hope someone will correct me if I'm wrong, but I believe you will
need to use has_many :through for this case -- HABTM isn't meant for 3-
way joins as far as I know.  You'll need something like this:

class Article < ActiveRecord::Base
  has_many :correlations
  has_many :tags, :through => :correlations
  has_many :users, :through => :correlations
end

(similarly for User and Tag)

class Correlation < ActiveRecord::Base
  belongs_to :article
  belongs_to :tag
  belongs_to :user
end

Then your sample controller code would become something like

user = User.new
tag = Tag.find(id)
article = Article.find(id)
user.correlations << Correlation.new(:tag => tag, :article => article)

Does that help?

Best,
--
Marnen Laibow-Koser
marnen@marnen.org
http://www.marnen.org
916981dcb90882ae0f984dc33de0a592?d=identicon&s=25 Felipe Vergara (Guest)
on 2009-01-02 00:28
(Received via mailing list)
that worked thank you very much!

On Sat, Dec 20, 2008 at 2:51 AM, Marnen Laibow-Koser
<marnen@marnen.org>wrote:

> > user.article << article
>
>  belongs_to :tag
> Does that help?
>
> Best,
> --
> Marnen Laibow-Koser
> marnen@marnen.org
> http://www.marnen.org
> >
>


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