That's a clean solution, however I don't know if it satisfies the fact
that "any Room might have many different combinations of Rounds"

Not sure I understand correctly, but if a room can have many
combination of rounds, and each combination of rounds has more than
one round, you could try this:

Room and Round models I assume you already have. A room can have many
round_combinations (create the RoundCombination model with a room_id.
Room :has_many :room_combinations, and
RoomCombination :belongs_to :room). Then create a join table between
round_combinations and rounds (HABTM).

You can use callbacks or validation to limit the relationship to two
rounds maximum.

On Feb 14, 11:42 pm, Maurício Linhares <mauricio.linha...@gmail.com>
wrote:
> Imagining that every round must belong to a room, here's a simple and
> straightforward implementation:
>
> class Room < ActiveRecord::Base
>   # this class needs a max_rounds and rounds_count integer columns
>   has_many :rounds
>
>   def accepts_more_rounds?
>     max_rounds < rounds_count
>   end
>
> end
>
> class Round < ActiveRecord::Base
>   # this class needs a room_id integer column
>   belongs_to :room, :counter_cache => true
>
>   validate_on_create :validate_rounds_count
>
>   protected
>
>   def validate_rounds_count
>     self.room.reload
>     unless self.room.accepts_more_rounds?
>       errors.add( :room, "has already met it's rounds limit" )
>     end
>   end
>
> end
>
> room = Room.create( :max_rounds => 2 )
> round_1 = room.rounds.create( :name => 'round 1' )
> round_2 = room.rounds.create( :name => 'round 2' )
> round_3 = room.rounds.create( :name => 'round 3' ) # this one isn't
> going to be created
> round_3.new_record? == true
>
> -
> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) 
> |http://blog.codevader.com/(en)
>
> On Sat, Feb 14, 2009 at 7:53 PM, Greg Brockman
>
> <gregory.brock...@gmail.com> wrote:
>
> > Hey all,
>
> > I have a pretty simple question, but I'm not sure of a good solution
> > to it.  Essentially, I want to provide a two-to-one mapping of models.
> >  I'm working on an application for a contest, where every (unordered)
> > combination of two Rounds is supposed to be assigned to one Room.  Any
> > Room might have many different combinations of Rounds, however.
>
> > What is the Right Way of doing this in Rails?  Maybe create a model
> > that holds the associated foreign keys?  Also, what would be a good
> > way to scale this out, if I wanted to be able to map unordered
> > n-element collections of Rounds to obtain a Room?
>
> > Thanks,
>
> > Greg
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to