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 7489603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:16:51+00:00 2026-05-29T15:16:51+00:00

I am trying to create several models that all pull from the same table.

  • 0

I am trying to create several models that all pull from the same table. How do I limit the table records in each model? And before you tell me to change my data structure, this is a reporting application that is pulling from a preexisting backing DB over which I have no control.

My table looks something like:

Vehicle_Table
id vehicle_type name
--------------------
1  Car          Foo
2  Car          Bar
3  Motorcycle   Baz
4  Car          Barf

And I want to build models for Car and Motorcycle like:

class Car < ActiveRecord::Base
  set_table_name 'Vehicle_Table'

end

and

class Motorcycle < ActiveRecord::Base
  set_table_name 'Vehicle_Table'

end

But I have no idea how to say, “Hey Active Record, I only want records where vehicle_type = motorcycle in the motorcycle model.”

I’m sure this is friggin’ obvious, but all of my Google searches return ways to FIND subsets in a model rather than RESTRICT a model to a specific subset of records.

  • 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-29T15:17:00+00:00Added an answer on May 29, 2026 at 3:17 pm

    This is called Single Table Inheritance (STI).

    If you had a column named type in your table, it would likely work automatically. But you can change this column name that Rails uses to tell types apart.

    http://api.rubyonrails.org/classes/ActiveRecord/Base.html

    Single table inheritance

    Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this:

    class Company < ActiveRecord::Base; end
    class Firm < Company; end
    class Client < Company; end
    class PriorityClient < Client; end
    

    When you do Firm.create(:name => "37signals"), this record will be saved in the companies table with type = “Firm”. You can then fetch this row again using Company.where(:name => ’37signals’).first and it will return a Firm object.

    So, try this code

    class Car < ActiveRecord::Base
      set_table_name 'Vehicle_Table'
      self.inheritance_column = :vehicle_type
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a single, one-off page that will combine information from several
we have model helper (used by several different models) called set_guids that sets self.theguid
I'm trying to use Django to display a table of information from across several
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
I have recently being trying to create a project which has several levels of
Trying to print out several different forms on the webpage with the data that
In my project, I have several models that use a number of scopes that

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.