# Has\_many and bi-directional relationships

**URL:** <https://www.ruby-forum.com/t/has-many-and-bi-directional-relationships/195097>\
**Category:** Rails\
**Created:** [September 16, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/has-many-and-bi-directional-relationships/195097 "2010-09-16T19:35:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pipplo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@pipplo](https://www.ruby-forum.com/u/pipplo)\
**Post date:** [September 16, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/has-many-and-bi-directional-relationships/195097/1 "2010-09-16T19:35:56Z")

</div>

I’m working on an app with a friend type system. I currently have  
setup like this with 2 tables.

users table keeps track of users

user\_relationships table keeps track of friendships. Now, there  
should be two rows for each relationship. This is to keep track of  
the directional nature of the relationship between users. Each time a  
relationship is made, both rows should be inserted into the table.

user\_1 user\_2 data  
user\_2 user\_1 data

So what I can’t decide is what is the best way to implement these in  
my rails app to be able to create a view of all user\_relationships  
with a given userid.

I can define a Relationship class, and mark User with has\_many. But  
that will only select the relationships based on one column right? So  
it would look in one column and find all rows from that column. so if  
I call User.relationships I will only have half of the relationship  
info right?

So what I think i’m looking for is a way to overload the  
User.relationships so that I can return a list of Relationships that I  
compiled by making two queries, one on each column?

I can think of some other options that might work in this instance  
(since i know there is always two rows of data I can just query on 1  
column, and then in the Relationship model handle the dual column  
nature of info), but I think it would still be interesting to know how  
to overload the function, or describe this behavior better in my  
model.

I hope this makes sense

Thanks

---

<div class="post-metadata">

**Author:** ![pipplo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@pipplo](https://www.ruby-forum.com/u/pipplo)\
**Post date:** [September 16, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/has-many-and-bi-directional-relationships/195097/2 "2010-09-16T19:47:25Z")

</div>

pipplo wrote:

> I’m working on an app with a friend type system. I currently have  
> setup like this with 2 tables.
> 
> users table keeps track of users
> 
> user\_relationships table keeps track of friendships. Now, there  
> should be two rows for each relationship. This is to keep track of  
> the directional nature of the relationship between users. Each time a  
> relationship is made, both rows should be inserted into the table.
> 
> user\_1 user\_2 data  
> user\_2 user\_1 data  
> […]

That’s not necessarily a great idea. Please see  
[Creating reflective has\_and\_belongs\_to - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/213468) .

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pipplo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@pipplo](https://www.ruby-forum.com/u/pipplo)\
**Post date:** [September 16, 2010, 8:32pm UTC](https://www.ruby-forum.com/t/has-many-and-bi-directional-relationships/195097/3 "2010-09-16T20:32:09Z")

</div>

How about something like this:

class User \< ActiveRecord::Base  
has\_many :relations, :foreign\_key =\> “from\_user”  
end

class Relation \< ActiveRecord::Base  
belongs\_to :from\_user, :class\_name =\> “User”, :foreign\_key =\>  
“from\_user”  
belongs\_to :to\_user, :class\_name =\> “User”, :foreign\_key =\> “to\_user”  
end

To print list of all friends of , say, first user ind DB, you can  
something  
like this:

User.first.relations.all.each { |r| puts r.to\_user.name }

Here is Migrations for two tables:

class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table :users do |t|  
t.string :name  
t.timestamps  
end  
end

def self.down  
drop\_table :users  
end  
end

class CreateRelations \< ActiveRecord::Migration  
def self.up  
create\_table :relations do |t|  
t.integer :from\_user  
t.integer :to\_user  
# other data specific to relation  
t.timestamps  
end  
end

def self.down  
drop\_table :relations  
end  
end

* * *

It may have some gotchas, but can be enhanced.

On Thu, Sep 16, 2010 at 11:17 PM, Marnen Laibow-Koser

---

<div class="post-metadata">

**Author:** ![pipplo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@pipplo](https://www.ruby-forum.com/u/pipplo)\
**Post date:** [September 16, 2010, 8:35pm UTC](https://www.ruby-forum.com/t/has-many-and-bi-directional-relationships/195097/4 "2010-09-16T20:35:40Z")

</div>

its done here

> **[\#163 Self-Referential Association - RailsCasts](http://railscasts.com/episodes/163-self-referential-association)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
