Rails: Using has_many :through associations, and the piggyback plugin to model dynamic forms
Tuesday, October 31st, 2006Note: this relies on you using EdgeRails .
I’m writing an application that will allow administrators to create dynamic forms for topics. The “dynamic form” design boils down to many to many relationship between topic_type_fields and topic_types. After a lot of getting to know what is out there with modeling many to many relationships in Ruby on Rails I settled on the has_many :through route.
Both has_and_belongs_to_many and has_many :through weren’t completey up to the task, but has_many :through was clearly winning the “hearts and minds” war within the Rails community. It’s a more intuitive way of expressing the relationship and turns out to flexible enough to do what I need. With a little help from the piggy_back plugin and some guidance from hasmanythrough.com’s blog I was able to get what I wanted.

