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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:23:01+00:00 2026-05-12T07:23:01+00:00

I have a many to one polymorphic rails association in my rails app for

  • 0

I have a many to one polymorphic rails association in my rails app for a ‘case’ model. There are many things that have cases, so I access the case each thing has by doing ‘thing_that_has_a_case.case’.

However, I’m trying to go the other way and I’m not sure how. I have access to the case object but I want to access the thing that is being cased. Does rails have a way to do this? I could do it with an ugly switch that does a different sql find for each type of object, but I was hoping rails would have a better way. Thanks!

  • 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-12T07:23:01+00:00Added an answer on May 12, 2026 at 7:23 am

    You can add something like this to your case model:

    belongs_to :parent, :polymorphic => true
    

    ..which assumes you have fields parent_id and parent_type in your case model (change the first argument to the belongs_to depending on how you have named these association fields). Once you have this, you should just be able to refer to case.parent for a given case instance.

    And, just to clarify, the one side of this relationship has something like:

    has_many :cases, :as => :parent, :dependent => :destroy
    

    (I’m assuming you already have that working based on your description)

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

Sidebar

Related Questions

for instance, I have 2 model Drummer and Video If I use one-to-many association
A Person can have many Events and each Event can have one polymorphic Eventable
I would like to have one model (event) that has multiple polymorphic HABTM associations
I have a many-to-one relationship that users can edit via checkboxes. PK of Foo
I have many cookie-cutter spring beans and don't want to explicitly define each one
I have one to many association Student entity to Subject entity. Student entity association
i have an entity Entity1 that have one to many relation with Entity2 as
I have created a polymorphic association around a model called status. Some contacts will
I have a polymorphic association ( contact_details ) in my Company model and I
Many statically typed languages have parametric polymorphism. For example in C# one can define:

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.