# A relationship question

**URL:** <https://www.ruby-forum.com/t/a-relationship-question/204454>\
**Category:** Rails\
**Created:** [March 12, 2011, 1:59pm UTC](https://www.ruby-forum.com/t/a-relationship-question/204454 "2011-03-12T13:59:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 12, 2011, 1:59pm UTC](https://www.ruby-forum.com/t/a-relationship-question/204454/1 "2011-03-12T13:59:53Z")

</div>

I have 3 tables; Feelings, Users, and Members. Both Feelings and Members  
belongs\_to Users with user\_id.

Both models Feelings and Members have belongs\_to :user in them. Feelings  
has has\_one :member, :through =\> :user.

Is this the right setup? How can I make a rails search condition in  
Feelings and get a column from members into that search?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [March 12, 2011, 2:21pm UTC](https://www.ruby-forum.com/t/a-relationship-question/204454/2 "2011-03-12T14:21:48Z")

</div>

On 12 March 2011 12:59, Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have 3 tables; Feelings, Users, and Members. Both Feelings and Members  
> belongs\_to Users with user\_id.
> 
> Both models Feelings and Members have belongs\_to :user in them. Feelings  
> has has\_one :member, :through =\> :user.

The above would be easier for us (well, me anyway) to understand if  
you showed the class definitions  
feeling.rb  
class Feeling \< ActiveRecord::Base  
belongs\_to :user

and so on. Also tell us the file names. Copy and paste from your  
code in case of typo errors.

> Is this the right setup? How can I make a rails search condition in  
> Feelings and get a column from members into that search?

I am not exactly sure what you want, but once you find Feeling object  
@feeling = Feeling.first for example, then, if the relationships are  
correct, the member can be accessed by  
@feeling.member  
so columns can be accessed by  
@feeling.member.name or whatever.

You can check whether it is working by using the rails console and  
typing things like  
f = Feeling.first  
f.member  
f.member.name

Colin
