Actually, by user I mean competitor, but you get the point... -----Original Message----- From: Harold <harold.gime...@gmail.com> Reply-to: rubyonrails-talk@googlegroups.com To: Ruby on Rails: Talk <rubyonrails-talk@googlegroups.com> Subject: [Rails] Re: Two-to-one mappings Date: Sun, 15 Feb 2009 14:55:27 -0800 (PST)
Does a user have many round_combinations? and a round_combination has many users? Seems like it. If so, first thought is to create another HABTM table between round_combinations and users. Then @user.round_combinations.each { |rc| rc.room } gives you the rooms... On Feb 15, 11:16 am, Greg <gregory.brock...@gmail.com> wrote: > Ah, so one follow up question: each Competitor in the competition is > signed up for two rounds, and needs to know which room he or she is > assigned. This is a has_one relationship; however, I'm not quite sure > of the right way to map it. Thoughts? > > Greg > > On Feb 15, 2:49 am, Harold A. Giménez Ch. <harold.gime...@gmail.com> > wrote: > > > Great! Glad it worked. > > > On Sun, Feb 15, 2009 at 1:57 AM, Greg <gregory.brock...@gmail.com> wrote: > > > > Cool! Harold, your solution strikes me as being exactly the way to do > > > it. I've implemented it, and things seem to be sailing smoothly. > > > Thanks a lot to both of you. > > > > Sincerely, > > > > Greg > > > > On Feb 14, 11:47 pm, Harold <harold.gime...@gmail.com> wrote: > > > > 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://alinhavado.wordpress.com/%28pt-br%29>| > > >http://blog.codevader.com/(en) <http://blog.codevader.com/%28en%29> > > > > > > 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 -~----------~----~----~----~------~----~------~--~---