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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:10:40+00:00 2026-06-13T06:10:40+00:00

I’ve seen some posts dealing with this, and am trying to determine the best

  • 0

I’ve seen some posts dealing with this, and am trying to determine the best solution.

Semantically, I want a Client model with a one-to-one relationship with a Survey. There are different kinds of surveys that have different fields but I want to share a significant amount of code between them. Because of the different fields I want different database tables for the surveys. There is no need to search across different types of surveys. It feels like I want the foreign key in the Client table for fast retrieval and potential eager-loading of the Survey.

So theoretically I think I want polymorphic has_one and multiple inheritance something like this:

class Client < ActiveRecord::Base
  has_one :survey, :polymorphic => true
end

class Survey
  # base class of shared code, does not correspond to a db table
  def utility_method
  end
end

class Type1Survey < ActiveRecord::Base, Survey
  belongs_to :client, :as => :survey
end

class Type2Survey < ActiveRecord::Base, Survey
  belongs_to :client, :as => :survey
end

# create new entry in type1_surveys table, set survey_id in client table
@client.survey = Type1Survey.create()

@client.survey.nil?            # did client fill out a survey?
@client.survey.utility_method  # access method of base class Survey
@client.survey.type1field      # access a field unique to Type1Survey

@client2.survey = Type2Survey.create()
@client2.survey.type2field     # access a field unique to Type2Survey
@client2.survey.utility_method

Now, I know Ruby does not support multiple inheritance, nor does :has_one support :polymorphic. So is there a clean Ruby way to achieve what I’m getting at? I feel like it’s right there almost…

  • 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-13T06:10:41+00:00Added an answer on June 13, 2026 at 6:10 am

    Here’s how I would do this:

    class Client < ActiveRecord::Base
      belongs_to :survey, :polymorphic => true
    end
    
    module Survey
      def self.included(base)
        base.has_one :client, :as => :survey
      end
    
      def utility_method
         self.do_some_stuff
      end
    end
    
    Type1Survey < ActiveRecord::Base
      include Survey
    
      def method_only_applicable_to_this_type
        # do stuff
      end
    end
    
    Type2Survey < ActiveRecord::Base
      include Survey
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.