ActiveRecord Association without Foreign Key

activerecord, associations, ruby-on-rails

Solution

Well, I can think of using `finder_sql` for that:

has_many :relations, :finder_sql => 'select * from relations right outer join users
    on relations.recipient_id = #{id} or relations.initiator_id = #{id}'

Apart from that, you can just write a method that will return a united array of the two relations associations', but you will lose the advantage of an association interface (phew).

Perhaps someone will come up with a better solution.

Problem

I am trying to build a relationship model between users. A user can either initiate a relation, or receive a relation from another user. Therefore, the relations table in the db has the foreign keys "initiator_id" and "recipient_id". Now, I can figure what relations the user initiated or received using the following associations: ``` has_many :initiated_relations, :foreign_key => :initiator_id, :class_name => 'Relation', :dependent => :destroy has_many :received_relations, :foreign_key => :recipient_id, :class_name => 'Relation', :dependent => :destroy ``` What I am trying to do, is build an association that will fetch me all relations that belong to a user (either initiated or received). Trying the following does not work, and complains about the lack of "user_id" field: ``` has_many :relations, :conditions => 'recipient_id = #{id} or initiator_id = #{id}' ``` How can I create an association that is solely based on the conditions field, without looking for the default foreign_key? Or is there perhaps a completely different approach to solving this?

Original source