5

I have implemented single table inheritance for a person class

class Person < ActiveRecord::Base

end


class Teacher < Person

end

class Student < Person

end

class Outsider < Person

end

And the create person seems to work creating Teacher, Student or Person according to the what is chosen in the form.select and the type attribute is added.

However, I seem to have broken the routes

<%= link_to 'Edit', edit_person_path(@deal) %> | <%= link_to 'Back', persons_path %>

They seem to point to teacher_path, student_path and outsider_path instead of person_path.

What changes need to be made in the routes?

Arc
  • 1,680
  • 6
  • 30
  • 57

2 Answers2

2

first generate controllers for your models...

rails generate controller Persons
rails generate controller Teachers
rails generate controller Students
rails generate controller Outsiders

then in routes.rb (rails 3)

resources :persons
resources :teachers
resources :students
resources :outsiders

gives you REST routes

e.g.

persons GET    /persons(.:format) {:action=>"index", :controller=>"persons"}
new_person GET    /person/new(.:format) {:action=>"new", :controller=>"persons"}
edit_person GET    /persons/:id/edit(.:format) {:action=>"edit", :controller=>"persons"}
person GET    /persons/:id(.:format) {:action=>"show", :controller=>"persons"} 
persons POST   /spersons(.:format) {:action=>"create", :controller=>"persons"}    
person PUT    /persons/:id(.:format) {:action=>"update", :controller=>"persons"}    
person DELETE /persons/:id(.:format) {:action=>"destroy", :controller=>"persons"}

the same for teacher, student and outsider

check rake routes or rake routes | grep teachers

codevoice
  • 474
  • 3
  • 6
  • 2
    i dont want to different controllers for each one – Arc Dec 14 '10 at 03:33
  • so you will lose REST add in routes.rb match 'teachers/' => "persons#index", :as => :teachers match 'teacher/:id(.:format)' => "persons#show", :as => :teachers and so on... – codevoice Dec 14 '10 at 07:53
  • 1
    this works, but is not DRY at all - you'll end up repeating all the controller code, and the view code over and over for each sub-class – Tilo Mar 08 '12 at 01:25
1

From my experience, it's better to use a single controller for all the STI models. If you're keeping your controllers DRY, you shouldn't need to have unique controller logic for each child class. Keep all that in the models.

resources :people

Your named routes will be like:

people_path
new_person
edit_person
person
etc...

Then you can use the same controller/views to manage these models. If you decide later to add new Person STI models, you won't have to make any significant updates to your code.

Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • 2
    This doesnot seem to work. For <%= link_to 'Edit', edit_person_path(@deal) %> it tries to find edit_teacher_path(@user) and fails – Arc Dec 14 '10 at 03:35
  • was struggling with this myself just now.. I want to treat my sub-models significantly differently in the views and would prefer not to have a bunch of conditional logic in either the controller or the view.. thoughts? – Kevin Davis Mar 24 '11 at 09:48