# Mixing Single Table Inheritance and RESTful routes

**URL:** <https://www.ruby-forum.com/t/mixing-single-table-inheritance-and-restful-routes/81089>\
**Category:** Rails\
**Created:** [January 3, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/mixing-single-table-inheritance-and-restful-routes/81089 "2007-01-03T19:06:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [January 3, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/mixing-single-table-inheritance-and-restful-routes/81089/1 "2007-01-03T19:06:58Z")

</div>

I am trying to implement a restful app, but I have found some problems  
with the design, when I have thought in implementig STI.

# I will try to explain. Only as a example, you may suppose this models

Class Person \< ActiveRecord::Base  
has\_many :widgets  
end

Class Customer \< Person  
end

Class Employee \< Person  
end

# Class Widget \< ActiveRecord::Base belongs\_to :person end

Then I would like to define these resources in config/routes.rb  
map.resources :customers do |customer|  
customer.resources :widgets  
end  
map.resources :employees do |employee|  
employee.resources :widgets  
end

Is this the correct way to abord this problem?  
Can I go in the REST style way for this case?

I will appreciate any comment of someone with some experience with these  
questios.

Thanks

Juan M. Cervera

---

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [January 4, 2007, 11:28am UTC](https://www.ruby-forum.com/t/mixing-single-table-inheritance-and-restful-routes/81089/2 "2007-01-04T11:28:32Z")

</div>

I have tried to find a solution, with no luck.  
There is no problem in defining the routes in routes.rb, but it seems  
that this will not work because I have only one set of named routes for  
the widgets resources.

Does anybody see a better way or an alternative for this problem?

It is important for me because in my real problem I have several classes  
that I can implement with STI, they are different resources in my app  
and each one needs its resftul controller. But they all have the same  
nested resource and I want only one restful controller, for all the  
cases.  
I pretend to be DRY in the design.

Anybody thinks there is a way to implement this?  
Please give me some advise.

Thanks in advance.

Juan M. cervera

---

<div class="post-metadata">

**Author:** ![jmcervera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@jmcervera](https://www.ruby-forum.com/u/jmcervera)\
**Post date:** [January 8, 2007, 6:24am UTC](https://www.ruby-forum.com/t/mixing-single-table-inheritance-and-restful-routes/81089/3 "2007-01-08T06:24:23Z")

</div>

I think I have tried very hard and still no luck.

I can have the restful routes working without problems  
map.resources :customers do |customer|  
customer.resources :widgets,  
:name\_prefix =\> ‘customer\_’  
end  
map.resources :employees do |employee|  
employee.resources :widgets  
:name\_prefix =\> ‘employee\_’  
end

Now I have named routes like customer\_new\_widget\_path or  
employee\_widgets\_path but I found difficult to be in the DRY way, with  
only on controller for widgets, mostly for the associated views.

Is possible that nobody has ever tried something like this?  
Or I can’t explain the question in a correct way, and nobody understands  
me.  
Well, I think I’m going to abandon this idea.

Thanks  
Juan M. Cervera
