# Self-referential has\_and\_belong\_to\_many

**URL:** <https://www.ruby-forum.com/t/self-referential-has-and-belong-to-many/123846>\
**Category:** Rails\
**Created:** [December 21, 2007, 7:55am UTC](https://www.ruby-forum.com/t/self-referential-has-and-belong-to-many/123846 "2007-12-21T07:55:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 21, 2007, 7:55am UTC](https://www.ruby-forum.com/t/self-referential-has-and-belong-to-many/123846/1 "2007-12-21T07:55:39Z")

</div>

Im having trouble developing the database structure for the following  
scenario. I have a table that only contains 2 fields for each  
object. It has a admirer\_id and an admired\_id. There is also a user  
table. Now all I want to do is go through the admirers table and for  
each admired\_id, take the corresponding admirer\_id and collect all the  
users that have a corresponding user\_id to this admirer\_id. Im having  
trouble setting up the database relationships and how I should go  
about this in general. I think this may involve using a self-  
referential has\_and\_belongs\_to\_many relationship? Any help/advice is  
greatly appreciated. Thanks! 😉
