Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:41:05+00:00 2026-05-20T07:41:05+00:00

I have a base class inherited by 2 others via Single Table Inheritance. I

  • 0

I have a base class inherited by 2 others via Single Table Inheritance. I want all subclasses to share the same controller/views for various reasons-the only real difference is in the model’s functionality.

However, when I try to use link_to “stuff”, instance_of_child I get complaints about being unable to find the correct page.

I’ve tried messing with match ‘/subclass’ => redirect(‘/parent’) but that yields weird links that make no sense. Any suggestions? I’m pretty new at rails, and I admit that my understanding of routes.rb is still limited-however, I’m not entirely sure that is even where I should be looking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:41:06+00:00Added an answer on May 20, 2026 at 7:41 am

    From http://www.alexreisner.com/code/single-table-inheritance-in-rails:

    If you’ve ever tried to add STI to an
    existing Rails application you
    probably know that many of your
    link_to and form_for methods throw
    errors when you add a parent class.
    This is because ActionPack looks at
    the class of an object to determine
    its path and URL, and you haven’t
    mapped routes for your new subclasses.
    You can add the routes like so, though
    I don’t recommend it:

    # NOT recommended:
    map.resources :cars,        :as => :vehicles, :controller => :vehicles
    map.resources :trucks,      :as => :vehicles, :controller => :vehicles
    map.resources :motorcycles, :as => :vehicles, :controller => :vehicles
    

    This only alleviates a particular
    symptom. If we use form_for, our form
    fields will still not have the names
    we expect (eg: params[:car][:color]
    instead of params[:vehicle][:color]).
    Instead, we should attack the root of
    the problem by implementing the
    model_name method in our parent class.
    I haven’t seen any documentation for
    this technique, so this is very
    unofficial, but it makes sense and it
    works perfectly for me in Rails 2.3
    and 3:

    def self.inherited(child)  
      child.instance_eval do
        def model_name
          Vehicle.model_name
        end
      end
      super 
    end
    

    This probably looks confusing, so let
    me explain:

    When you call a URL-generating method
    (eg: link_to(“car”, car)), ActionPack
    calls model_name on the class of the
    given object (here car). This returns
    a special type of string that
    determines what the object is called
    in URLs. All we’re doing here is
    overriding the model_name method for
    subclasses of Vehicle so ActionPack
    will see Car, Truck, and Motorcycle
    subclasses as belonging to the parent
    class (Vehicle), and thus use the
    parent class’s named routes
    (VehiclesController) wherever URLs are
    generated. This is all assuming you’re
    using Rails resource-style (RESTful)
    URLs. (If you’re not, please do.)

    To investigate the model_name
    invocation yourself, see the Rails
    source code for the
    ActionController::RecordIdentifier#model_name_from_record_or_class
    method. In Rails 2.3 the special
    string is an instance of
    ActiveSupport::ModelName, in Rails 3
    it’s an ActiveModel::Name

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that private variables in a base class are technically inherited by
I have a base class with a property which (the get method) I want
I have a base class in which I want to specify the methods a
I have a base class with a virtual method, and multiple subclasses that override
I have a class (Descendant1) that inherits from a base class (BaseClass). An instance
I have a base class object array into which I have typecasted many different
I have a base class with an optional virtual function class Base { virtual
I have a base class that represents a database test in TestNG, and I
I have a base class that has a private static member: class Base {
I have a base class vehicle and some children classes like car, motorbike etc..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.