# Many-to-many self-referential connection

**URL:** <https://www.ruby-forum.com/t/many-to-many-self-referential-connection/75087>\
**Category:** Rails\
**Created:** [November 7, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/many-to-many-self-referential-connection/75087 "2006-11-07T13:29:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaskas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9dc877/32.png) [@vaskas](https://www.ruby-forum.com/u/vaskas)\
**Post date:** [November 7, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/many-to-many-self-referential-connection/75087/1 "2006-11-07T13:29:35Z")

</div>

Hi, folks!

I’m trying to model the following kind of relationship in my  
application:

Each Topic has many related Topics. The relationship is characterised  
by “similarity” parameter.

That’s what I have in DB:

create\_table “topics” do |t|  
t.column “name” :string  
end

create\_table “related\_topics” do |t|  
t.column “topic\_id”, :integer, :null =\> false  
t.column “related\_topic\_id”, :integer, :null =\> false  
t.column “similarity” :integer  
end

and in the model:

class Topic \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :related\_topics,  
:class\_name =\> “Topic”,  
:join\_table =\> “related\_topics”,  
:association\_foreign\_key =\> “related\_topic\_id”,  
:foreign\_key =\> “topic\_id”

What is bad about this reference is that the similarity parameter is  
unaccessible!  
I can’t affect it neither during \<\<'ing of related\_topics, or any other  
way.

E.g. when I do:  
tp = Topic.find(1)  
tp.related\_topics.find(1).similarity = 5  
tp.save

the similarity does not save.

I guess I should use join models and :through, but I have no idea how  
to access similarity parameter in that case as well.

Please point me at any solutions to the problem. Thank you.

---

<div class="post-metadata">

**Author:** ![vaskas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9dc877/32.png) [@vaskas](https://www.ruby-forum.com/u/vaskas)\
**Post date:** [November 7, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/many-to-many-self-referential-connection/75087/2 "2006-11-07T13:55:39Z")

</div>

this sounds like a job for a “has\_many :through =\>” association

read this blog post on self-referential has\_many :through associations,  
i think its quite exactly what you need. This blog covers other  
related\_topics ( lol ) about this as well, have fun

[http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through](http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through)

---

<div class="post-metadata">

**Author:** ![vaskas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9dc877/32.png) [@vaskas](https://www.ruby-forum.com/u/vaskas)\
**Post date:** [November 7, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/many-to-many-self-referential-connection/75087/3 "2006-11-07T21:14:01Z")

</div>

Thorsten, thanks for your answer.  
I’ve tried this approach, but I can’t modify the “similarity” parameter  
anyway.

If you look, Josh also has a “flow” parameter for his graphs - it’s  
exactly the same (in terms of db organization), what I want. But  
unfortunately, he writes nothing about flow’s further fate.

So, still looking for the solution.
