ActiveRecord relationships for a join table linking two records of the same table?

Has_and_belongs_to_many is not really in use anymore; I'd use has_many :through instead class Character destroy has_many :characters, :through => :links has_many :source_links, :class_name => "Link", :foreign_key => "to_character_id", :dependent => :destroy has_many :source_characters, :class_name => "Character", :through => :destination_links end class Link "Character", :foreign_key => "to_character_id" end Note the :dependent => :destroy options - these will delete the links when the character is deleted. The naming is right - from the character's point of view source_links are links to that character. So now you can do: character.

Characters # characters I link to @character. Links # links I have to other characters @character. Source_characters # characters that link to me @character.

Source_links # links other characters have to me.

Has_and_belongs_to_many is not really in use anymore; I'd use has_many :through instead. Class Character destroy has_many :characters, :through => :links has_many :source_links, :class_name => "Link", :foreign_key => "to_character_id", :dependent => :destroy has_many :source_characters, :class_name => "Character", :through => :destination_links end class Link "Character", :foreign_key => "to_character_id" end Note the :dependent => :destroy options - these will delete the links when the character is deleted. The naming is right - from the character's point of view, source_links are links to that character.So now you can do: @character.

Characters # characters I link to @character. Links # links I have to other characters @character. Source_characters # characters that link to me @character.

Source_links # links other characters have to me.

I think you want something like the following: class Character "Link", :foreign_key => "from_character_id" has_many :inbound_links, :class_name => "Link", :foreign_key => "to_character_id" end class Link "Character", :foreign_key => "from_character_id" belongs_to :to_character, :class_name => "Character", :foreign_key => "to_character_id" end You can read about all your options on ActiveRecord associations at api.rubyonrails.org/classes/ActiveRecord....

I think what you are really going for here is a self referential HABTM relationship using a join table so if you had a join table create_table :character_links do |t| t. Integer :character_id t. Integer :linked_character_id t.

Timestamps #if you want to know when the relationship was created end Then you would have class Characters "Characters", :join_table => :character_links, :foreign_key => "character_id", :associated_foreign_key => "linked_character_id" if you needed outgoing links and incoming links then you could just do class Characters "Characters", :join_table => :character_links, :foreign_key => "character_id", :associated_foreign_key => "linked_character_id" has_and_belongs_to_many :incoming_links, :class_name => "Characters", :join_table => :character_links, :foreign_key => "linked_character_id", :associated_foreign_key => "character_id" Just switched the foreign_key and associated_foreign_key This eliminates the need for you to have a seperate Links model This is air code(not tested).

Agile Web Development with Rails presents a solution in the section "Using Models as Join Tables" but for a join table joining two different tables. In my case my join table Links join records of a single table, Characters.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions