# Association Help

**URL:** <https://www.ruby-forum.com/t/association-help/142163>\
**Category:** Rails\
**Created:** [July 4, 2008, 12:06am UTC](https://www.ruby-forum.com/t/association-help/142163 "2008-07-04T00:06:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hirodusk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6f9a4e/32.png) [@hirodusk](https://www.ruby-forum.com/u/hirodusk)\
**Post date:** [July 4, 2008, 12:06am UTC](https://www.ruby-forum.com/t/association-help/142163/1 "2008-07-04T00:06:20Z")

</div>

Hello,

Let me setup the scenario

class User \< ActiveRecord::Base  
belongs\_to :house  
end

class House \< ActiveRecord::Base  
has\_many :users  
end

My question is, how can i implement the roomates association from the  
user model? I’d like to do current\_user.roomates and get a list of  
current\_user.house.users _minus_ the current\_user.

Thanks!

-Adam

---

<div class="post-metadata">

**Author:** ![hirodusk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6f9a4e/32.png) [@hirodusk](https://www.ruby-forum.com/u/hirodusk)\
**Post date:** [July 4, 2008, 1:53am UTC](https://www.ruby-forum.com/t/association-help/142163/2 "2008-07-04T01:53:30Z")

</div>

@House.find(:first).users - [current\_user]

in your controller somewhere.

---

<div class="post-metadata">

**Author:** ![hirodusk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6f9a4e/32.png) [@hirodusk](https://www.ruby-forum.com/u/hirodusk)\
**Post date:** [July 4, 2008, 2:16am UTC](https://www.ruby-forum.com/t/association-help/142163/3 "2008-07-04T02:16:30Z")

</div>

Haven’t tested this but I’m thinking something like:

class User \< ActiveRecord::Base  
belongs\_to :house

def roommates  
User.find(:all,  
:conditions =\> [‘house.id = ? and user.id \<\> ?’,  
self.house, self],  
:joins =\> “inner join houses on house.user\_id =  
users.id”)  
end

end

On Jul 3, 6:06 pm, Adam W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hirodusk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6f9a4e/32.png) [@hirodusk](https://www.ruby-forum.com/u/hirodusk)\
**Post date:** [July 4, 2008, 10:43am UTC](https://www.ruby-forum.com/t/association-help/142163/4 "2008-07-04T10:43:24Z")

</div>

> @House.find(:first).users - [current\_user]
> 
> in your controller somewhere.

I would put it in the models if i was to use a method like this

class User \< AR::Base  
belongs\_to :house

def roomates  
house.users - [self]  
end  
end

the problem with this aproach is that you cant do anything with the  
rooomates apart from seeing them. you have no User.roomates \<\<  
user.new or any of the Ar methods.

So… I would put it into the association, having house as a join  
table between Users and Users… something on the lines (and note i  
say on the lines because you would probably have to change the  
associations you have to accomplish this.) of:

class user

has\_many :roomates, :through =\> :house, :class\_name =\> ‘User’

end
