a work on process

Rails’ Improved Join Model Creation

You are reading a post by James Stewart entitled Rails’ Improved Join Model Creation. It was posted on 19 August 2006 at 8:26 pm.

You can find more posts by returning to the index.

Filed under: Notes
Tagged: , , ,

Jeremy Kemper has just checked in a change to Edge Rails that fixes my biggest frustration with has_many :through. Since it’s appearance a couple of versions back, has_many :through has been a great addition to the rails associations toolkit, making the use of join models infinitely cleaner. But until recently it was rather cumbersome to create those relationships.

For example, if I wanted to add an author to a book where the relationship is defined as:

has_many :authors, :through => :involvements, 
  :class_name => 'Contributor'

The simplest it could be to find and create an author and set up a link was:

book.involvements.create!(
  :author => Contributor.find_or_create_by_name(name))
book.authors(true)

Now it’s as simple as:

book.authors < < Contributor.find_or_create_by_name(name)

The one gotcha comes if your book object is a new record, and so doesn’t yet have an ID. Trying the above in that case will raise ActiveRecord::HasManyThroughCantAssociateNewRecords.

Since in my particular case I’m masking the association behind a proxy attribute, I considered getting around the problem by dynamically creating (and subsequently removing) after_save callbacks, but for now I’ve simply changed my controller to create the associations after the record is saved. Maybe Edge Rails will have some more options to offer us before long.

Thanks to Josh Susser for the original report. Check out his post for more detail and examples.

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Posts

 

No Comments »

No comments yet.

TrackBack URI

Leave a comment

Login Method

OpenID

Anonymous