# Has\_many :through - to self, bi-directional

**URL:** <https://www.ruby-forum.com/t/has-many-through-to-self-bi-directional/122337>\
**Category:** Rails\
**Created:** [December 6, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/has-many-through-to-self-bi-directional/122337 "2007-12-06T19:09:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodieboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecc23a/32.png) [@goodieboy](https://www.ruby-forum.com/u/goodieboy)\
**Post date:** [December 6, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/has-many-through-to-self-bi-directional/122337/1 "2007-12-06T19:09:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![goodieboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecc23a/32.png) [@goodieboy](https://www.ruby-forum.com/u/goodieboy)\
**Post date:** [December 13, 2007, 12:23am UTC](https://www.ruby-forum.com/t/has-many-through-to-self-bi-directional/122337/2 "2007-12-13T00:23:26Z")

</div>

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
