Forum: Ruby on Rails habtm recusive

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.
F7891bf864cadfb23971078e759df133?d=identicon&s=25 Steven Bristol (stevenbristol)
on 2006-01-04 14:58
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
1f4ea34d7b38aa9ae1582be630d1ec11?d=identicon&s=25 Reginald Braithwaite (Guest)
on 2006-01-04 18:17
(Received via mailing list)
On 1/4/06, steven bristol <stevenbristol@gmail.com> 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...

--
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
F7891bf864cadfb23971078e759df133?d=identicon&s=25 Steven Bristol (stevenbristol)
on 2006-01-04 19:20
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...
>

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


Thanks,
steve
6944576a4252ea5303f0b978f8604a30?d=identicon&s=25 Reginald Braithwaite (Guest)
on 2006-01-04 20:48
(Received via mailing list)
On 1/4/06, steven bristol <stevenbristol@gmail.com> wrote:
> > http://lists.squeakfoundation.org/pipermail/squeak...
> >
>
> 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
6944576a4252ea5303f0b978f8604a30?d=identicon&s=25 Reginald Braithwaite (Guest)
on 2006-01-04 21:09
(Received via mailing list)
On 1/4/06, steven bristol <stevenbristol@gmail.com> 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
F7891bf864cadfb23971078e759df133?d=identicon&s=25 Steven Bristol (stevenbristol)
on 2006-01-04 21:13
Great ideas! Thanks!

steve

P.S. I did find that link interesting.
This topic is locked and can not be replied to.