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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:24:42+00:00 2026-06-07T01:24:42+00:00

I have the TestVisual class that is inherited by the Game class : class

  • 0

I have the TestVisual class that is inherited by the Game class :

class TestVisual < Game
  include MongoMapper::Document  
end

class Game
  include MongoMapper::Document         

  belongs_to :maestra  

  key :incorrect,         Integer
  key :correct,           Integer
  key :time_to_complete,  Integer

  key :maestra_id, ObjectId

  timestamps!

end

As you can see it belongs to Maestra.

So I can do Maestra.first.games which returns []

But I can not to Maestra.first.test_visuals because it returns undefined method test_visuals

Since I’m working specifically with TestVisuals, that is ideally what I would like to pull, but still have it share the attributes of its parent Game class.

Is this possible with Mongo. If it isn’t or if it isn’t necessary, is there any other better way to reach the TestVisual object from Maestra and still have it inherit Game ?

  • 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-06-07T01:24:44+00:00Added an answer on June 7, 2026 at 1:24 am

    Single Collection Inheritance (SCI) in MongoMapper auto-generates selection,
    ex., the following produce the same results.

    p Game.where(_type: 'TestVisual').all
    p TestVisual.all
    

    See also mongomapper/lib/mongo_mapper/plugins/sci.rb – MongoMapper::Plugins::Sci::ClassMethods#query

    However, MongoMapper does not auto-generate associations for subclasses based on the base class’ associations,
    and I don’t think that this should be expected.

    Note that SCI places subclasses and base classes in the same MongoDB collection.
    If this is not what you want, you should consider other mechanisms for modularity.

    You can define the following method yourself for an association accessor method, perhaps this is sufficient for your purposes?
    For other association methods like append or push, the parent methods are probably workable.

    class Maestra
      include MongoMapper::Document
      key :name, String
      many :games
    
      def test_visuals
        games.where(_type: 'TestVisual')
      end
    end
    

    test/unit/test_visual_test.rb

    require 'test_helper'
    
    def ppp(obj)
      puts obj.inspect.gsub(/, ([^#])/, ",\n\t\\1").gsub(/, #/, ",\n #")
    end
    class TestVisualTest < ActiveSupport::TestCase
      def setup
        Maestra.delete_all
        Game.delete_all
      end
    
      test "inheritance" do
        maestra = Maestra.create(name: 'Fiona')
        maestra.games << Game.create(incorrect: 1, correct: 9, time_to_complete: 60)
        maestra.games << TestVisual.create(incorrect: 2, correct: 8, time_to_complete: 61)
        ppp maestra.games.to_a
        ppp maestra.test_visuals.to_a
      end
    end
    

    output

    Run options: --name=test_inheritance
    
    # Running tests:
    
    [#<Game _id: BSON::ObjectId('4ff7029a7f11ba6e43000002'),
        _type: "Game",
        correct: 9,
        created_at: Fri,
        06 Jul 2012 15:22:02 UTC +00:00,
        incorrect: 1,
        maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
        time_to_complete: 60,
        updated_at: Fri,
        06 Jul 2012 15:22:02 UTC +00:00>,
     #<TestVisual _id: BSON::ObjectId('4ff7029a7f11ba6e43000003'),
        _type: "TestVisual",
        correct: 8,
        created_at: Fri,
        06 Jul 2012 15:22:02 UTC +00:00,
        incorrect: 2,
        maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
        time_to_complete: 61,
        updated_at: Fri,
        06 Jul 2012 15:22:02 UTC +00:00>]
    [#<TestVisual _id: BSON::ObjectId('4ff7029a7f11ba6e43000003'),
        _type: "TestVisual",
        correct: 8,
        created_at: Fri,
        06 Jul 2012 15:22:02 UTC +00:00,
        incorrect: 2,
        maestra_id: BSON::ObjectId('4ff7029a7f11ba6e43000001'),
        time_to_complete: 61,
        updated_at: Fri,
        06 Jul 2012 15:22:02 UTC +00:00>]
    .
    
    Finished tests in 0.026661s, 37.5080 tests/s, 0.0000 assertions/s.
    
    1 tests, 0 assertions, 0 failures, 0 errors, 0 skips
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a look at one of my websites: moskah.com The problem is that it
Have a simple contact us XPage created. Have server side validation in place that
Have a network location that shows paths in the 8.3 short format. I need
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
Have some dates in my local Oracle 11g database that are in this format:
have span that may or may not have text content. is there a way

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.