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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:54:01+00:00 2026-06-14T04:54:01+00:00

I have a standard polymorphic relation set up like: #Foo has_many: bar, as: foobar

  • 0

I have a standard polymorphic relation set up like:

#Foo
has_many: bar, as: foobar

#Qux
has_many: bar, as: foobar

#Bar
belongs_to :foobar, polymorphic: true

I want to be able to enforce a model that has_many: bar, as: foobar to have certain attributes for example a name, so I can do something like:

bar.foobar.name

and know that it doesn’t matter which model foobar is at that moment. (Since all foobars have a name attribute)

Is there a way to do this or do I have to test for it with responds_to? everytime I want to use it.

  • 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-14T04:54:02+00:00Added an answer on June 14, 2026 at 4:54 am

    You can provide defaults by creating a base class that Foo and Qux extend. Instead of

    class Foo < ActiveRecord::Base
      has_many: bar, as: foobar
    end
    
    class Qux < ActiveRecord::Base
      has_many: bar, as: foobar
    end
    

    you could do something like

    class Foobar < ActiveRecord::Base
      has_many: bar, as: foobar
    end
    
    class Foo < Foobar; end
    
    class Qux < Foobar; end
    

    You could then create a default accessor method for the attributes you want to be consistent for those classes extending Foobar.

    class Foobar < ActiveRecord::Base
      has_many: bar, as: foobar
    
      def name
        "some default name"
      end
    end
    
    class Foo < Foobar
      def name
        "some overloaded name for Foo"
      end
    end
    
    class Qux < Foobar; end
    

    You can overload name as shown above in Foo, or omit it as shown in Qux. Regardless, a name attribute will be available, even if it’s only the default from Foobar.

    foo = Foo.new
    puts foo.name  # "some overloaded name for Foo"
    
    qux = Qux.new
    puts qux.name # "some default name"
    

    If you’re looking to create more of a contract through an interface or abstract class as is common in other languages, Ruby doesn’t support either. You can however use a mixin that you’d include in Foo and Qux to throw an Exception if name is not defined. Read an example of this sort of implementation here.

    Note on the chance these models are supposed to have an associated name column in the table, you will likely want to incorporate read_attribute(:name) somewhere in your overloaded name method. See the docs on read_attribute

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

Sidebar

Related Questions

I have a table Foo that has a polymorphic belongs_to association called bar .
I have a mobile site running with jQuery Mobile. I want to have standard
I have a standard table: I want to use jQuery to reformat the table,
I have a standard list view setup with backbone that looks something like below.
I have a standard code like below to validate xml against xsd, but it
I have a standard form array in PHP like this. I need to account
Does Java have standard functions for security like in php htmlspecialchars , strip_tags ?
I have Standard version of eclipse Galileo. Now I want to develop enterprise applications
I have standard multi-level menu like this one: <ul id=nav> <li><a href=#>Home</a></li> <li><a href=#>Dropdown</a>
I have a standard polymorphic relationship and I need to know who its parent

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.