Tutorial :Single Table Inheritance routing?


I have single table inheritance working just fine for my app. I have two user subtypes (Athlete and Company) that inherit the super-type User.

Let's say I am listing all users, and want a link to each user's profile from this list. I want to link to the athletes controller if the type is athlete and the companies controller if the type is company. Is there a standard Rails way to this? Maybe some routing tricks?


you can even do that much simpler, Rails recognizes which type of user it has to deal with, so let's say you have the instance variable @user wich can either be an Athlete or a Company, you can just do that

= link_to "Profile", @user  

BAM! Rails magic!


<% User.find(:all).each do |user| %>    <%= link_to "user", eval("#{user.type.underscore}_path(user)") %>  <% end %>  

This will generate a path according to the type of the user (stored in type field). Don't forget to add the type of users to your routes configuration.

I hope this helps.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »