Habtm recusive

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

On 1/4/06, steven bristol [email protected] 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


Reginald Braithwaite
http://www.braithwaite-lee.com/weblog/

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

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

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

Thanks,
steve

On 1/4/06, steven bristol [email protected] wrote:

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/

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

Great ideas! Thanks!

steve

P.S. I did find that link interesting.

On 1/4/06, steven bristol [email protected] 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/

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