On Oct 5, 2009, at 22:33 , Jonathan Christensen wrote: > I've been looking around the internet and I don't see an obvious > good way to do what I'd like to do, so I thought I'd ask the group... > > I have an object, lets call it Room, that has many dates of > availability. And conversely, on any given day of the year, there > may be many Rooms available. > > I'm trying to decide the best way to model this with ActiveRecord. > From a db perspective, it seems like it would be easy to just have a > table called rooms_days (or something like that) that has two > columns. room_id:int and day:date. > > But because of my lack of experience, I'm not quite sure whether > this is sufficient and how to model it in my ActiveRecord classes. > I started to imagine saying the Room has_and_belong_to_many :days. > but then I don't have a model called Day, and I don't really want to > make a table that has only one date column in it because that would > add an additional join that seems unnecessary. > > I think maybe there's some way to make a Day model, but not have a > table underneath it, but still tell it to update the RoomDates table > via the association. Does anyone have experience trying something > like this?
rooms table with room names. bookings table with room_id and date: unique index on the pair, and if you search on both date and room and there are a lot of bookings, you may want the reverse index also, but don't do that until you find it's too slow without the index. You assume that if a room-date pair doesn't appear in that table, it is not booked. You can use joins to find booked rooms, outer joins to find unbooked rooms, and a union of the two (or any of several other tactics) to give you all your rooms and whether they're booked given various date conditions or room conditions. Should work pretty well. Regards, Guyren G Howe Relevant Logic LLC guyren-at-relevantlogic.com ~ http://relevantlogic.com ~ +1 512 784 3178 Ruby/Rails, REALbasic, PHP programming PostgreSQL, MySQL database design and consulting Technical writing and training Read my book, Real OOP with REALbasic: <http://relevantlogic.com/oop-book/about-the-oop-book.php > --~--~---------~--~----~------------~-------~--~----~ SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby -~----------~----~----~----~------~----~------~--~---
