# Habtm recusive

**URL:** <https://www.ruby-forum.com/t/habtm-recusive/89381>\
**Category:** Rails\
**Created:** [January 4, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/habtm-recusive/89381 "2006-01-04T14:58:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenbristol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@stevenbristol](https://www.ruby-forum.com/u/stevenbristol)\
**Post date:** [January 4, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/habtm-recusive/89381/1 "2006-01-04T14:58:41Z")

</div>

I have a people table:

CREATE TABLE people (  
id int(10) unsigned NOT NULL auto\_increment,  
first\_name varchar(75) default NULL,  
middle\_name varchar(75) default NULL,  
last\_name varchar(75) default NULL,  
PRIMARY KEY (id)  
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO\_INCREMENT=1272 ;

and a people\_people table:

CREATE TABLE people\_people (  
person\_id int(11) unsigned NOT NULL,  
person2\_id int(11) unsigned NOT NULL,  
PRIMARY KEY (person\_id,person2\_id)  
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

I’m not sure the right way to do this in rails. In my person model, I  
added the following:

```
has_and_belongs_to_many :people, {
	:association_foreign_key=>'person2_id',
	:foreign_key=>'person_id'
	}

```

but that only allows the relationship to go one way. Obviously I need it  
to bo both ways.

Any ideas?

thanks,  
steven a bristol

---

<div class="post-metadata">

**Author:** ![stevenbristol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@stevenbristol](https://www.ruby-forum.com/u/stevenbristol)\
**Post date:** [January 4, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/habtm-recusive/89381/2 "2006-01-04T18:17:36Z")

</div>

On 1/4/06, steven bristol [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a people table:  
> …  
> and a people\_people table:

Is this relationship symmetrical or asymmetrical?

For example, “likes” as in “Bob likes Jane” is not necessarily  
symmetrical, there can be cases where Jane does not like Bob.

For an asymmetrical relationship from a table to itself, I would  
consider ‘promoting’ the relationship into a model in its own right.  
You lose the habtm goodness in rails, but you can do things like:

class FondOfRelationship \< ActiveRecord::Base  
belongs\_to :subject, :class\_name =\> :person  
belongs\_to :object, :class\_name =\> :person  
end

(I may have the syntax wrong, but I’m sure you get the idea).

> CREATE TABLE people (  
> …  
> first\_name varchar(75) default NULL,  
> middle\_name varchar(75) default NULL,  
> last\_name varchar(75) default NULL,

You may find this informative:

[http://lists.squeakfoundation.org/pipermail/squeak-dev/2003-April/057254.html](http://lists.squeakfoundation.org/pipermail/squeak-dev/2003-April/057254.html)

–  
Reginald Braithwaite  
[http://www.braithwaite-lee.com/weblog/](http://www.braithwaite-lee.com/weblog/)

“What in the wide, wide world of sports is going on here?!”  
–Slim Pickens as “Taggart”, Blazing Saddles

---

<div class="post-metadata">

**Author:** ![stevenbristol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@stevenbristol](https://www.ruby-forum.com/u/stevenbristol)\
**Post date:** [January 4, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/habtm-recusive/89381/3 "2006-01-04T19:20:00Z")

</div>

and a people\_people table:

> Is this relationship symmetrical or asymmetrical?

It is symmetrical, I found asymmetrical vary easy to accomplish, but the  
symmetrical not so straight-forward.

> You may find this informative:
> 
> [http://lists.squeakfoundation.org/pipermail/squeak-dev/2003-April/057254.html](http://lists.squeakfoundation.org/pipermail/squeak-dev/2003-April/057254.html)

That was a very interesting read, but quite off topic. Did you mean to  
send that link?

Thanks,  
steve

---

<div class="post-metadata">

**Author:** ![stevenbristol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@stevenbristol](https://www.ruby-forum.com/u/stevenbristol)\
**Post date:** [January 4, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/habtm-recusive/89381/4 "2006-01-04T20:48:39Z")

</div>

On 1/4/06, steven bristol [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > [http://lists.squeakfoundation.org/pipermail/squeak-dev/2003-April/057254.html](http://lists.squeakfoundation.org/pipermail/squeak-dev/2003-April/057254.html)
> 
> That was a very interesting read, but quite off topic. Did you mean to  
> send that link?

You had mentioned specifying first name, middle name, and last name  
columns; I thought you might find the author’s perspective  
interesting.

–  
Reginald Braithwaite  
[http://www.braithwaite-lee.com/weblog/](http://www.braithwaite-lee.com/weblog/)

“I didn’t know what an activist was. We were taught history like it was  
over.”  
–Guerilla gardener David Meslin

---

<div class="post-metadata">

**Author:** ![stevenbristol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@stevenbristol](https://www.ruby-forum.com/u/stevenbristol)\
**Post date:** [January 4, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/habtm-recusive/89381/5 "2006-01-04T21:13:32Z")

</div>

Great ideas! Thanks!

steve

P.S. I did find that link interesting.

---

<div class="post-metadata">

**Author:** ![stevenbristol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cab0a1/32.png) [@stevenbristol](https://www.ruby-forum.com/u/stevenbristol)\
**Post date:** [January 4, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/habtm-recusive/89381/6 "2006-01-04T21:09:42Z")

</div>

On 1/4/06, steven bristol [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It is symmetrical, I found asymmetrical vary easy to accomplish, but the  
> symmetrical not so straight-forward.

Great problem.

I can suggest several hacks to try:

1. Paramaterize the habtm call with :insert\_sql and :delete\_sql so it  
saves/deletes a mirror image row in the people\_people table. So when  
you save [1, 2] you also save [2, 1].
2. Paramaterize the habtm call with :finder\_sql so that when it  
performs a search it also gets the ‘backlinks’.

–  
Reginald Braithwaite  
[http://www.braithwaite-lee.com/weblog/](http://www.braithwaite-lee.com/weblog/)

If you can’t describe what you are doing as a process, you don’t know  
what you are doing.  
–W. Edward Deming
