views:

24

answers:

1

I am setting up the Single Table Inheritance, using ContactEvent as the Model that ContactEmail, ContactLetter, and ContactCall will all inherit.

But I'm stumped on how to create the routing and the controller.

For example, let's say I want to create a new ContactEvent with type Email.

I would like a way to do the following:

new_contact_event_path(contact, email)

This would take the instance from Contact model and from Email model.

Inside, I would imagine the contact_event_controller would need to know...

   @contact_event.type = (params[:email]) # get the type based on what was passed in?
   @contact_event.event_id = (params[:email]) #get the id for the correct class, in this case Email.id

Just not sure how this works....

A: 

I would have a controller (and maybe views) for each of your resource types. So add a controller for ContactEmail one for ContactLetter etc. Don't bother with one for the base class ContactEvent. Then your paths would read something like:

new_contact_email_path(@contact) or new_contact_letter_path(@contact)

The controller actions would then use the right model for that they represent, ie:

@contact_email = ContactEmail.new(params[...])

If you can keep your three types of resources separate rather than trying to pass the type in and building the right object in one controller you should find life much easier. The downside is you may need more links/forms/views in the front end, however depending on your application that may not be a bad thing from the user's perspective.

tsdbrown
How would I reduce some of the redundancy? The reason I am going towards this is so that only one controller has the code, right now, I have alot of duplication....
Angela
Most basic RESTFUL controllers look exactly the same minus a few small differences. The Model being used, flash message etc. If you have identified three similar resources each one deserves the right to its own existence even if they are very very similar. You could look at using a plugin such as resources_controller if you would like to reduce this (and normal rails) duplication. From the views point of view you can pull anything that is generic into a shared partial. You could pass in the one or two objects that need to be unique depending on where it is getting rendered from.
tsdbrown
Hmm...so then I guess I shouldn't even try to consolidate them as a single table inheritance?
Angela
That's not what I'm saying. That will depend on your data, if your three models contain the same fields/more or less the same fields then STI would be the way to go. I'm just saying although that's how you've got the data modelled it doesn't mean you can't have controllers/views for each of them.
tsdbrown