# Has\_two, has\_three, etc

**URL:** <https://www.ruby-forum.com/t/has-two-has-three-etc/134956>\
**Category:** Rails\
**Created:** [April 16, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/has-two-has-three-etc/134956 "2008-04-16T16:11:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [April 16, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/has-two-has-three-etc/134956/1 "2008-04-16T16:11:21Z")

</div>

I was wondering what the community thought about an extension to the  
associations mechanism which allowed declarations like:

class Game \< AR::Base

has\_two :referees

end

I have run across this situation fairly frequently. An object has a  
defined number of subobjects of a particular type. In this situation I  
don’t necessarily want to use habtm or has\_many :through. The join  
table just adds overhead to things like validation and ensuring that  
there are not more than 2 referees attached to the game, etc.

This would be implemented db fields like referee\_1\_id,  
referee\_2\_id,etc…

This approach simplifies forms and object creation in the contexts  
where it is appropriate.

If I had a list of referees, I could just do a collection\_select for  
each referee, rather than having to worry about link up other records  
through a join table.

Obviously this approach only applies to situations where there are a  
defined number of subobjects, but I find this to be a fairly common  
occurrence.

Thoughts, comments?

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [April 16, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/has-two-has-three-etc/134956/2 "2008-04-16T16:23:43Z")

</div>

On 16 Apr 2008, at 15:10, Andrew S. wrote:

> I have run across this situation fairly frequently. An object has a  
> defined number of subobjects of a particular type. In this situation I  
> don’t necessarily want to use habtm or has\_many :through. The join  
> table just adds overhead to things like validation and ensuring that  
> there are not more than 2 referees attached to the game, etc.

I can’t say I’ve felt the need for something like this very often. But  
stick in a plugin and who knows, it might take off!

Fred

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [April 16, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/has-two-has-three-etc/134956/3 "2008-04-16T16:59:35Z")

</div>

I’m with Fred: I haven’t really needed something like this but I can  
see the value of it. If you were gonna roll your own plugin for this,  
I’d suggest generalizing it a bit more. Maybe something like:

has\_some :referees, :count =\> 2

Then it would look for referee\_1\_id and referee\_2\_id, just like you  
wrote. But then you could dynamically define the number.

Of course, it occurs to me that you are very tightly wedded to the DB  
in this case. I.e. if you want to change to three referees, you’d need  
to update the DB to add another FK field. And, the “has\_many” or  
“has\_one” puts the FK on _other_ table. It’s “belongs\_to” that puts  
the FK on the current table.

So maybe a better way to think about this is that it uses a normal  
has\_many or habtm relationship but adds some constraints on it. I.e.  
if I set :count =\> 2, then trying to add a 3rd to the association  
would fail.

Just some thoughts off the top of my head.

-Danimal

On Apr 16, 8:22 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [November 24, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/has-two-has-three-etc/134956/4 "2008-11-24T14:07:48Z")

</div>

Hello,

you could simply put in your Game model :

belongs\_to :referee\_1, :class\_name =\> ‘Referee’  
belongs\_to :referee\_2, :class\_name =\> ‘Referee’

But the problem is in the Referee model, what you would want is :

has\_one :game

but that doesnt work because you have in fact two possible columns for  
the foreign key in the games table, leading to all the problems “Clever  
Neologism” mentionned.

Now if you want to be able to get the game the referee is assigned to  
(but ONLY in read mode), you could put in your Referee model :

def game  
Game.find(:first, :conditions=\>"referee\_1\_id = “+id.to\_s+” or  
referee\_2\_id = "+id.to\_s)  
end

of course the language itself doesn’t make it read only… so be careful  
🙂

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [April 16, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/has-two-has-three-etc/134956/5 "2008-04-16T17:58:51Z")

</div>

On Apr 16, 10:10 am, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> defined number of subobjects of a particular type. In this situation I  
> don’t necessarily want to use habtm or has\_many :through. The join  
> table just adds overhead to things like validation and ensuring that  
> there are not more than 2 referees attached to the game, etc.

You are going to have to add “belongs\_to\_many” as well as “has\_two”…  
if your game table has referee ids in it, then a game belongs\_to a  
referee (not what you would expect, but that’s the way it is). The  
problem will come in when you say referee.game = somegame (or  
referee.games \<\< game) or game.referees \<\< referee, because a referee  
has\_two (or five, whatever) games. Which column should these  
operations reference and change in the games table? In other words,  
if you assign a referee to a game, which of the already assigned  
referees should be replaced? It’s going to get ugly quick: you’ll  
have to have some kind of either pre-named function to do that for you  
(that would be hardcoded), or pass in a block. It’s a lot of work for  
a thin veneer of syntactic sugar… unless you have 10 or 20 of  
something, you really just need to write the 10 lines of code. This  
is probably why this feature doesn’t exist.

Either you would have to break bi-directional referencing between your  
models (clearly not optimal), or write the replacement policy yourself  
(what you are having to do now anyway). It’s just not something  
where there is really only one correct way to do it. The same applies  
to modifying habtm with a :limit parameter or something like that,  
which if you had to do it some way, that’s what I would use, and throw  
an exception if you try to add too many things. I.e. force the user  
to take a referee away before adding if there are already two.

Here’s the proper (IMO) way to do it:

:habtm :referees do  
def addreferee  
(guard against too many referees)  
end  
#I have never tried this, but you might be able to overload  
operators using this  
def \<\<(ref)  
end  
end

or:

:habtm :referees, :before\_add =\> :validate\_refs

def validate\_refs()  
#Check count  
end

In the Rails RDocs, checkout  
[ActiveRecord::Associations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html).

Relevant sections are “Association Callbacks” and “Association  
Extensions”. If you do decide to write a plugin, this is probably the  
file to look at and mimic (probably using one of the methods above to  
modity HABTM relationship with a :limit parameter).
