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

  • Home
  • SEARCH
  • 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 652739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:18:19+00:00 2026-05-13T22:18:19+00:00

When working with a polymorphic association, is it possible to run an include on

  • 0

When working with a polymorphic association, is it possible to run an include on submodels that are only present in some types?

Example:

class Container
  belongs_to :contents, :polymorphic => true
end
class Food
  has_one :container
  belongs_to :expiration
end
class Things
  has_one :container
end

In the view I’m going to want to do something like:

<% c = Containers.all %>
<% if c.class == Food %>
  <%= food.expiration %>
<% end %>

Therefore, I’d like to eager load the expirations when I load up c, because I know I will need every last one of them. Is there any way to do so? Just defining a regular :include gets me errors because not all enclosed types have a submodel expiration.

  • 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-13T22:18:20+00:00Added an answer on May 13, 2026 at 10:18 pm

    Edited Answer

    I recently found out that Rails supports eager loading of polymorphic associations when you filter by the polymorphic type column. So there is no need to declare fake associations.

    class Container
      belongs_to :content, :polymorphic => true
    end
    

    Now query the Container by container_type.

    containers_with_food = Container.find_all_by_content_type("Food", 
                               :include => :content)
    
    containers_with_thing = Container.find_all_by_content_type("Thing", 
                               :include => :content)
    

    Old Answer

    This is a hack as there is no direct way to include the polymorphic objects in one query.

    class Container
      belongs_to :contents, :polymorphic => true
      # add dummy associations for all the contents.
      # this association should not be used directly
      belongs_to :food
      belongs_to :thing
    end
    

    Now query the Container by container_type.

    containers_with_food = Container.find_all_by_content_type("Food", 
                               :include => :food)
    
    containers_with_thing = Container.find_all_by_content_type("Thing", 
                               :include => :thing)
    

    That results in two SQL calls to the database ( actually it is 4 calls as rails executes one SQL for every :include)

    There is no way to do this in one SQL as you need different column set for different content types.

    Caveat: The dummy associations on Content class should not be used directly as it will result in unexpected results.

    E.g: Lets say the first object in the contents table contains food.

    Content.first.food # will work
    Content.first.thing
    

    The second call will not work. It might give you a Thing object with the same id as the Food object pointed by Content.

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

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One good approach is to create a pool of sessions… May 15, 2026 at 3:59 pm
  • Editorial Team
    Editorial Team added an answer UPDATE T1 SET T1.Inci = T2.Inci FROM T1 INNER JOIN… May 15, 2026 at 3:59 pm
  • Editorial Team
    Editorial Team added an answer It sounds like you're taking a pretty inappropriate approach to… May 15, 2026 at 3:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.