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

  • Home
  • SEARCH
  • 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 6066621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:31:13+00:00 2026-05-23T09:31:13+00:00

Consider the following example code: def Product < ActiveRecord::Base end def Book < Product

  • 0

Consider the following example code:

def Product < ActiveRecord::Base
end

def Book < Product
end

(I gave up finding a name for this kind of relationship. I thought Book is a “child model” and Product is the “parent,” but that is incorrect. For example, a Comment model object linked to another Comment model object is what would be a “child-parent” relationship. If you have a better way to describe the relationship of the models in my code above, you’re welcome to write it down here. Either way, this is not really why I’m here.)

Of course, with the code above, the Book model will share the same database table than Product, “products.” The model type will be saved as a string in the table’s “type” column. So, calling Product.create will insert a row in the table with the type value set to “Product” and using with Book.create will do the same but with the value of “Book.”

What puzzles me, however, is calling Book.last will return the last row with the type “Book,” as I expect it, but Product.last will return the last row no matter what type, including “Product” and “Book,” even though I wish to only get the last “Product.”

I tried defined a default_scope in Product which I override in my Book, but that causes more problems than it solves, and messed up my named scopes in Product which are inherited by Book.

Is there a way to make Product.last return the last object of type “Product” and avoid any objects of type “Book”?

Thanks in advance for your answers.

  • 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-23T09:31:14+00:00Added an answer on May 23, 2026 at 9:31 am

    The basic misunderstanding here seems to be between the concept of “relationships” or “associations” (such as “has_one”, “belongs_to”, etc.), and the object oriented concept of inheritance.

    Read up on this: http://rubylearning.com/satishtalim/ruby_inheritance.html

    Based on your example, I think you’ve got the concept of associations figured out, but you’re confusing it with inheritance, which isn’t the same, and you’re not quite understanding here.

    When a class inherits from another class (in your example, Book inherits from Product), the “child” class gets all the functionality of the “parent” class, plus any new functionality that it adds on top of that.

    Because of that, all Books are Products, but not all Products are Books.

    The reason Product.last returns the last row in the products table regardless of type, is because all Products (including Books) are products. So, a Book IS a product, therefore if the last Product is a Book, that will be returned.


    Ok, so the easiest way to accomplish what you’re trying to do here is to define all types of Products as sub-types of Product, and never use Product.last.

    Let’s say you’re keeping track of Books, CDs, and Cars

    class Book < Product
    class CD < Product
    class Car < Product

    This way, when you need to get the newest Book, CD, or Car added to the database, you do Book.last, CD.last, or Car.last. If you just need to know the newest Product of any type, you do Product.last.

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

Sidebar

Related Questions

Consider the following example F# code: type mytype() = member this.v = new OtherClass()
Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing
Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Consider the following example: class A(): def __init__(self): self.veryImportantSession = 1 a = A()
Consider the following example code: http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html I can do scrollwheel: false on a mapOptions
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following example: interface IPropertyCollection { public MethodWrapper GetPropertySetterByName(string name); //<<-- I want
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,

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.