Rich associations: OUT, join models: IN

— April 2, 2006 at 14:25 PDT

I noticed this comment in the Rails trac timeline [4123]

* DEPRECATED: Using additional attributes on has_and_belongs_to_many associations. Instead upgrade your association to be a real join model [DHH]

Couple that with the change to make habtm records with additional attributes :read_only, and it looks like the time to switch to has_many :through is now.

By the way, I've got a little list of articles I want to write showing various ways of using join models. If you have a request for something you want to see, leave a comment and I'll add it to the list.

5 commentsassociations, rails

  1. Chris Stephens2006-04-03 17:34:24

    Hi Josh,

    I've got one for you! I'm a newbie to Rails so this might be quite simple. In database terms it is - it's the Rails modelling with ActiveRecord I'm having trouble with.

    I've got four entities: plays, performances, venues and seats.

    There are two venues, in each of which there are multiple plays, each of which has several performances. Each venue has a couple of hundred seats, each of which has a state: free, reserved, and reserved-for-ten-minutes-during-an-online-sale.

    Where I am having problems is around the many-to-many relationship between seats and performances. I have a table of performances and a table of seats and a join table for the seat/performance relationship which includes a column for seat state.

    I believe this is a valid, normalised way to do this (and I'm sorry I don't have code to share but I am away from my Mac at the moment) but I can't get the ActiveRecord syntax right. I want to be able to do things like the following:

    • get all free seats for a performance of play 1 on 2006-04-30 (which takes place in venue 2)

    • reserve seat 158 for the performance of play 2 on 2006-05-01 (which takes place in venue 1)

    • create a new performance (as admin) of play 2 in venue 1 with a whole new set of seat associations, all marked as free

    Any help with this would be much appreciated!

    BTW I have been doing Ruby on Rails for 2 weeks now and I'm loving it...

    Thanks in advance for any help you can offer,


  2. David Willis2006-04-05 11:12:58

    Hi Josh,

    If rich associations via attributes on join tables is being deprecated, and we are being directed to migrate to using join models, perhaps you can answer this question for all the database, rail newbies like me: does one need to make changes to the table structure to move to a join model?

    I've been running into issues trying to convert my HABTM with attributes into using has_many :through, mostly involving attemting to remove joins.

    I'm so new at this I don't know what it is that I don't know.


  3. Josh Susser2006-04-05 11:24:06

    @David: Yes. Probably the only thing you need to do to your habtm join table to make it work as a join model is to add an id column to use as the primary key. I think doing that as a migration while preserving data isn't completely straightforward, or at least someone on the email list was having trouble with it. I haven't had to tackle that myself so I don't have a solid recipe for doing the migration, but I think I have an idea that would work fairly simply. I'll put it on the list of articles to write...

  4. Ian2006-06-23 21:31:11

    Not sure if you'll even see this, but since you asked for suggestions of other ways to use the technique, I thought I'd suggest you write up a little article based on the Agile book, page 241 (third printing, 2005) where the "David Says..." section talks about replacing an articles_users relationship with a new model called Reading.

  5. las Vegas2006-09-12 11:14:15

    It's critical to be lost! Goodness, the japanese method truculently repaid along one sad thing. Department rid some gambling. I slapped that casinos around that online casino. The impressed casino underlay prior to this atomic boy.

Sorry, comments for this article are closed.