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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:57:58+00:00 2026-06-03T03:57:58+00:00

I am developing a controller that creates a model with a polymorphic belongs_to association.

  • 0

I am developing a controller that creates a model with a polymorphic belongs_to association. What I do now to find the model it belongs to is as follows:

 def find_polymorphic_model(classes)
   classes_names = classes.map { |c| c.name.underscore + '_id' }

   params.select { |k, v| classes_names.include?(k) }.each do |name, value|
     if name =~ /(.+)_id$/
       return $1.classify.constantize.find(value)
     end
   end

  raise InvalidPolymorphicType
end

Where classes is an array of valid types for the association.

The problem with this approach is that I have to remember in the controller which types are allowed for the model I am creating.

Is there a way to find which types are allowed for a certain polymorphic belongs_to association? Or maybe I am doing this wrong and I should not let a polymorphic controller be exposed without nesting it in the polymorphic resource (in the router)?

I also think there may be problems with the fact that Rails lazy loads classes, so to be able to find out this thing I would have to explicitly load all models at initialization time.

  • 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-03T03:58:00+00:00Added an answer on June 3, 2026 at 3:58 am

    For your validation you don’t have to get all the possible polymorphic types. All you need is to check if the specified type (say, the value of taggable_type attribute) is suitable. You can do it this way:

    # put it in the only_polymorphic_validator.rb. I guess under app/validators/. But it's up to you.
    class OnlyPolymorphicValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
            polymorphic_type = attribute.to_s.sub('_type', '').to_sym
            specified_class = value.constantize rescue nil
            this_association = record.class.to_s.underscore.pluralize.to_sym
    
            unless(specified_class.reflect_on_association(this_association).options[:as] == polymorphic_type rescue false)
                record.errors[attribute] << (options[:message] || "isn't polymorphic type")
            end
        end
    end
    

    And then use:

    validates :taggable_type, only_polymorphic: true
    

    to check whether :taggable_type contains a valid class.

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

Sidebar

Related Questions

I am developing an application that currently has a View Controller (call it ViewControllerX).
I am developing a game that uses a different controller for each level. It
I have an object that I am developing a controller for that has many
I am developing a complex form that updates several records of one model at
I'm developing a controller program used to run a humanoid kidsize robot. The OS
I'm developing a HTTP front controller, based on the pattern of Martin Fowler (
I'm developing a Grails App. I have about 20 Controllers right now and there
Ok guys I am developing an iPhone app I have a Model class which
Developing rails has become lots of fun over the year that I've done it,
At which layer (Model, View, Controller) of MVC should permission logic be handled? Let

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.