Hello, I have set up that uses single table inheritance. I have a number of model subclasses following the example in AWDWR that uses Manager < Employee < Person. First question. Do I need to create controller classes for each? I don''t think I do because a single controller should be able to shunt the data between the models and the views. Second question. Say I have a view that shows a list of all people in the db. It shows first name, last name, and a ''show'' link to view that persons details. For each type I would like to show a specific view, i.e. the manager_view, the employee_view etc. If I have a single controller with a show action and I pass it the person as a parameter how do I get the controller to show the correct view? Thank you, Ryan Glover -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Glover wrote:> > Hello, > > I have set up that uses single table inheritance. I have a number of > model subclasses following the example in AWDWR that uses Manager < > Employee < Person. > > First question. Do I need to create controller classes for each? I > don''t think I do because a single controller should be able to shunt the > data between the models and the views. > > Second question. Say I have a view that shows a list of all people in > the db. It shows first name, last name, and a ''show'' link to view that > persons details. For each type I would like to show a specific view, > i.e. the manager_view, the employee_view etc. If I have a single > controller with a show action and I pass it the person as a parameter > how do I get the controller to show the correct view? > > Thank you, > Ryan Glover >First question: no, not necessary to have separate controllers. Using some convention of config naming (see below) it should work out nicely using the fact that @someinstance.class will return the STI subclass and @someinstance.class.base_class will return the, er, base class Second question: You can do something in your controller like: def show @item = Person.find(params[:id]) # where Person is your base class render :template => "/people/#{@item.class.base_class}_view" # assuming the controller is called people and the templates employee_view, manager_view end Alternatively if there''s a lot of common stuff in the show templates, just have one show template and use partials in the view to show the bit that''s unique to the STI subclasses. HTH Chris --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Glover wrote:> First question. Do I need to create controller classes for each? I > don''t think I do because a single controller should be able to shunt the > data between the models and the views. >No, you can use one controller if it makes sense in your application.> Second question. Say I have a view that shows a list of all people in > the db. It shows first name, last name, and a ''show'' link to view that > persons details. For each type I would like to show a specific view, > i.e. the manager_view, the employee_view etc. If I have a single > controller with a show action and I pass it the person as a parameter > how do I get the controller to show the correct view? >Lots of ways. The first that comes to my mind would be ... render :template => "people/#{@person.type.to_s.underscore}" (assuming /app/views/people/manager.rhtml, etc.) but I''m sure someone will come up with an option in even fewer lines of code. ;-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---