Has_many :through - to self, bi-directional

Hi,

I’ve got a model (Feature) that is “related” to other Features. The
name of the has_many is “relations”.

There is a join table called FeatureRelation, which holds a feature_id
and a related_feature_id. Something like:

class Feature
has_many :relations, :class_name=>‘FeatureRelation’
end

But I don’t want just the relations that a feature has created. I want
the relations that other features have created to and from. Here
is an example that might show what I mean (doesn’t work):

class Feature

has_many :relations, :class_name=>‘FeatureRelation’,
:conditions=>‘feature_id
= #{id} OR related_feature_id = #{id}’
end

But this still doesn’t do it. How can I combine the features being
related to and from together? Like:

Feature.find(1).relations == ‘All features relations with feature_id
or related_feature_id equal to 1’

Thanks

I also need this exact relationship, but I think it’s impossible due to
the way associations are set up (ie. they relate on a single key). I
haven’t looked closely to be sure, but my instinct is that it has to be
done manually, so I am implementing it without formal associations (ie.
just building the methods I actually need in my model).

M.W. Mitchell wrote:

Hi,

I’ve got a model (Feature) that is “related” to other Features. The
name of the has_many is “relations”.

There is a join table called FeatureRelation, which holds a feature_id
and a related_feature_id. Something like:

class Feature
has_many :relations, :class_name=>‘FeatureRelation’
end

But I don’t want just the relations that a feature has created. I want
the relations that other features have created to and from. Here
is an example that might show what I mean (doesn’t work):

class Feature

has_many :relations, :class_name=>‘FeatureRelation’,
:conditions=>‘feature_id
= #{id} OR related_feature_id = #{id}’
end

But this still doesn’t do it. How can I combine the features being
related to and from together? Like:

Feature.find(1).relations == ‘All features relations with feature_id
or related_feature_id equal to 1’

Thanks