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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:51:23+00:00 2026-05-27T03:51:23+00:00

For example there are some models class Model_1 < ActiveRecord::Base has_many :images, :as =>

  • 0

For example there are some models

class Model_1 < ActiveRecord::Base
   has_many :images, :as => :imageable
end

class Model_2 < ActiveRecord::Base
   # doesn't have has_many association
end
...
class Image < ActiveRecord::Base
    belongs_to :imageable, :polymorphic => true
end

How can I check that model has has_many association? Something like this

class ActiveRecord::Base
    def self.has_many_association_exists?(:association)
        ...
    end
end

And it can be used so

Model_1.has_many_association_exists?(:images) # true
Model_2.has_many_association_exists?(:images) # false

Thanks in advance

  • 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-27T03:51:23+00:00Added an answer on May 27, 2026 at 3:51 am

    What about reflect_on_association?

    Model_1.reflect_on_association(:images)
    

    Or reflect_on_all_associations:

    associations = Model_1.reflect_on_all_associations(:has_many)
    associations.any? { |a| a.name == :images }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there some principles of organizing classes into namespaces? For example is it OK
I will show the problem with a example. There is some text in the
I'm using PHP in order to redirect some search query. There is some example
I have this model: class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent =
There are some good examples on how to calculate word frequencies in C#, but
I am working through some examples in a WCF book. There is a Host
I will explain problem with an example: There is two table in my database,
At http://docs.jquery.com/UI/Tabs#Example there is a short HTML example of how to use JQuery UI
On the Groovy example page there is an example of how to use Groovy
I need to explain this by example: Is there a best practice or preference

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.