I think this should be:
class FrogPair < ActiveRecord::Base belongs_to :friend, :class_name=>‘Frog’, :foreign_key=>‘species_friend’
belongs_to :foe, :class_name=>‘Frog’, :foreign_key=>‘species_foe’ end
That allows you to do
fp = FrogPair.find(2) and then fp.friend.name fp.foe.name
If you want access to a list of pairs where a specific frog is the friend or foe respectively, do:
class Frog < ActiveRecord::Base has_many :pairs_as_friend, :class_name=>‘FrogPair’, :foreign_key=>‘species_friend’
has_many :pairs_as_friend, :class_name=>‘FrogPair’, :foreign_key=>‘species_foe’ end
Thinking of it, I think you have the semantics of your data model wrong. If you want to express relationships between different frog species, you are better off separating it into two different tables:
Frog FrogFriends FrogFoes
where FrogFriends and FrogFoes may look something like this:
frog_id friend_id and frog_id foe_id
The common problem with these reciprocal relationships is that you will probably have to insert two records into each of the tables:
some_frog_species_instance.add_friend( another_frog_species_instance ) should result in two “friend” entries, with the values reversed, so you can find all friends of a certain species by the condition “FROM frog_friends WHERE frog_id = some_frog_species_id”, rather than “FROM frog_friends WHERE frog_id=some_frog_species_id OR friend_id=some_frog_species_id”
Cheers, Max