>
> 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.
>
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
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
:)
--
Posted via http://www.ruby-forum.com/.