# Two has\_one's?

**URL:** <https://www.ruby-forum.com/t/two-has-ones/88481>\
**Category:** Rails\
**Created:** [March 11, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/two-has-ones/88481 "2007-03-11T21:59:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![siegi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@siegi](https://www.ruby-forum.com/u/siegi)\
**Post date:** [March 11, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/two-has-ones/88481/1 "2007-03-11T21:59:55Z")

</div>

Hi,

I’d like to associate two model objects such as

class Pair \< ActiveRecord::Base  
has\_one :good\_member, :class\_name=\>‘Member’  
has\_one :bad\_member, :class\_name=\>‘Member’  
end

class Member \< ActiveRecord::Base  
belongs\_to :pair  
end

so that a pair has two members where one is good and the other is bad.

As far as I understand, the association information would be kept in  
the “member” database table as a foreign key to the associated “pair”  
record, e.g. member\_id

Since the association information is not kept within the “pair” table

- where it would be possible to distinguish between the good and the  
bad member, how can I do the above association?

It would be quite comfortable to declare this kind of relationship in  
the style above.

Many thanks in advance,  
Siegi

---

<div class="post-metadata">

**Author:** ![siegi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@siegi](https://www.ruby-forum.com/u/siegi)\
**Post date:** [March 12, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/two-has-ones/88481/2 "2007-03-12T17:27:06Z")

</div>

> so that a pair has two members where one is good and the other is bad.
> 
> As far as I understand, the association information would be kept in  
> the “member” database table as a foreign key to the associated “pair”  
> record, e.g. member\_id
> 
> Since the association information is not kept within the “pair” table
> 
> - where it would be possible to distinguish between the good and the  
> bad member, how can I do the above association?

# 

# sql table has columns:

# memberable\_type, :string

# memberagble\_id, :integer

# 

class Member \< ActiveRecord::Base  
belongs\_to :memberable, :polymorphic =\> true  
end

class GoodMember \< Member  
end

class BadMember \< Member  
end

class Pair \< ActiveRecord::Base  
has\_one :good\_member, :as =\> :memberable  
has\_one :bad\_member, :as =\> :memberable  
end
