Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Doing a has_and_belongs_to_many against a single table (See related posts)

Found via the RoR wiki. Solution courtesy of Jakob S.

Assuming a schema like such:

create table node (
  id integer primary key,
  name varchar(32)
);

create table nodelink (
  source_id integer,
  destination_id integer
);


A Rails association like this would supposedly do the job:

has_and_belongs_to_many :nodes, :join_table => 'nodelink', :association_foreign_key => 'destination_id', :foreign_key => 'source_id'

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts