# Is possbile limit the number of object in has\_many option?

**URL:** <https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902>\
**Category:** Rails\
**Created:** [April 30, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902 "2009-04-30T18:51:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [April 30, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902/1 "2009-04-30T18:51:02Z")

</div>

(my post was deleted i don’t know why)  
Hi. I have a question.  
for example, when model records about man, suppose man can marry with  
1 woman.  
then i think code will be like this  
class Man \< ActiveRecord::Base  
has\_many: wife  
end

Because one man can marry with up to 1 woman( 0 or 1 )  
it can’t be 1:1 relationship, didn’t it?  
but at the same time more than 1 wife is not allowed.  
then how can i model this relationship into rails code?  
has\_many :limit option is fit for this relation?

Thanks.  
and i wish it is not deleted again. If it should be deleted please let  
me know why it should be.

---

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [April 30, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902/2 "2009-04-30T18:57:29Z")

</div>

Did you consider

has\_one :wife

And in Wife.rb

belongs\_to :man

---

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [April 30, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902/3 "2009-04-30T19:18:35Z")

</div>

On Apr 30, 2009, at 12:56 PM, Commander J. wrote:

> Did you consider
> 
> has\_one :wife
> 
> And in Wife.rb
> 
> belongs\_to :man

Or:  
class Wife  
has\_many :marriages  
has\_one :current\_marriage, :class\_name =\> ‘Marriage’, :conditions  
=\> { :current =\> true }  
has\_one :husband, :through =\> :current\_marriage  
end

class Marriage  
belongs\_to :wife  
belongs\_to :husband  
end

class Husband  
has\_many :marriages  
has\_one :current\_marriage, :class\_name =\> ‘Marriage’, :conditions  
=\> { :current =\> true }  
has\_one :wife, :through =\> :current\_marriage  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [April 30, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902/4 "2009-04-30T19:30:17Z")

</div>

thanks for replies.  
it gives me some thought about making active record association.

then what if the man is a single?  
husband may not have current\_marriage.  
i was thinking that has\_one means it should have one object.  
But as you know not every man will marry.

please more advice for this novice.

---

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [April 30, 2009, 8:08pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902/5 "2009-04-30T20:08:58Z")

</div>

On Apr 30, 2009, at 1:29 PM, serenobs wrote:

> thanks for replies.  
> it gives me some thought about making active record association.
> 
> then what if the man is a single?  
> husband may not have current\_marriage.  
> i was thinking that has\_one means it should have one object.  
> But as you know not every man will marry.
> 
> please more advice for this novice.

Then, man.current\_marriage will be nil as will man.wife

has\_one really defines a 0/1 and has\_many a 0/n relation.

-Rob

---

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [May 1, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902/6 "2009-05-01T15:33:36Z")

</div>

has\_one does indeed mean has a maximum of one. The belongs\_to side  
means has exactly one.

I don’t know (because I never tried it) wjay happens if you put  
"belongs\_to on both sides - i.e. make something exactly 1:1. Obviously  
not relevant in this case, but I could see situations where for  
performance reasons you may want that kind of partition - or when  
wrapping an external database.

C

On Apr 30, 1:08 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![serenobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@serenobs](https://www.ruby-forum.com/u/serenobs)\
**Post date:** [May 1, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/is-possbile-limit-the-number-of-object-in-has-many-option/166902/7 "2009-05-01T20:13:28Z")

</div>

On May 1, 2009, at 9:32 AM, Chris Bird wrote:

> has\_one does indeed mean has a maximum of one. The belongs\_to side  
> means has exactly one.
> 
> I don’t know (because I never tried it) what happens if you put  
> "belongs\_to on both sides - i.e. make something exactly 1:1. Obviously  
> not relevant in this case, but I could see situations where for  
> performance reasons you may want that kind of partition - or when  
> wrapping an external database.
> 
> C

Belongs\_to implies the presence of the foreign key so you can’t have  
it on both models (unless you let them be out-of-sync while being  
created).

Has\_one will only find one, but there could be many records that have  
the right foreign key. Just take any has\_many association and change  
it to has\_one (making the symbol singular, of course) and it will  
“just work” even though all the original records from the has\_many are  
actually still there.

Notice how I defined has\_many :marriages and has\_one :current\_marriage  
that both use the same Marriage model. Those all belongs\_to :wife  
(and belongs\_to :husband, too)

-Rob
