views:

27

answers:

1

I'm developing a rails project where I have one data model with multiple fields that are collection selects. I'd like to create another model to represent all of these collection select fields. So, for instance, my main data model has three collection select fields -- one for county, one for category, and one for classification. I could separate these into three separate data models, but that seems redundant since they all share the same characteristics. They have a type and a value, like a county is a county and it has a value of let's say Sonoma, just as category has a type of category and a value of let's say Winery. If you've ever used Drupal, I'm basically looking for the behavior of the taxonomy functionality.

So you see my dilemma: I need to separate these fields into three separate fields but they have very similar data structures. Any suggestions would be greatly appreciated.

+1  A: 

This is a perfect case for single-table inheritance. Your problem is screaming for it.

x1a4
So basically I need to create a base model and then create a separate model for each data type I have and tell those models to inherit from my base model. That seems fairly logical.
Tristan O'Neil
Yep, and add a `type` property that's a string to that base model, which is where Rails stores the correct class to turn that row into when it's pulled from the database.
x1a4
Alright I think I get it for the most part have it setup but when I try to display the value stored it shows the id and not the actual value. Is there something I need to do to be able to access the stored value rather than the id of the value.
Tristan O'Neil
Also how do you get this all to work with a single controller?
Tristan O'Neil
I'm not sure what you mean by 'access the stored value rather than the id'As far as single controller, the best method is to map the resources using routes: `map.resources :foobars, :controller => :mah_controller` `map.resources :bazbars, :controller => :mah_controller`
x1a4