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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:32:34+00:00 2026-06-07T03:32:34+00:00

I have two models where A has_many B. If I load A including associated

  • 0

I have two models where A has_many B. If I load A including associated B as such:

a = A.find(:first, include: :bs)

a.inspect only shows the attributes of a:

 => "#<A id: 1, name: \"Test\", created_at: \"2012-07-02 21:50:32\", updated_at: \"2012-07-02 21:50:32\">"

How can I do a.inspect such that it displays all associated a.bs?

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

    You can’t do that by default. It might create too many problems and side effects with inspecting objects. However you could extend inspect yourself with something like this:

    class A < ActiveRecord::Base
      ...
      def inspect
        [super, bs.inspect].join("\n")
      end
    end
    

    Note though that that’s not very clever, since it will force the loading of bs every time you inspect an A instance. So maybe you want to be smarter and do something like this:

    def inspect
      [super, bs.loaded? ? bs.inspect : nil].compact.join("\n")
    end
    

    This will only inspect bs if it’s already preloaded (with :include for example).

    Or maybe you want to create a super_inspect instead that does everything automatically. You could extend ActiveRecord::Base with something like:

    class ActiveRecord::Base
      def deep_inspect
        ([inspect] + self.class.reflect_on_all_associations.map { |a|
          self.send(a.name).inspect
        }).compact.join("\n  ")
      end
    end
    

    This will automatically look up all the associations with reflect_on_all_associations, and if the association is loaded it will call inspect on that.

    Now you can modify the above code however you want to create your own customized inspect, or just extend the current inspect if you like. Anything is possible with a little bit of code.

    Here is an example of an updated version that is a bit smarter:

    class ActiveRecord::Base
      def deep_inspect
        ([inspect] + self.class.reflect_on_all_associations.map { |a|
          out = ""
          assoc = self.send(a.name)
          # Check for collection
          if assoc.is_a?(ActiveRecord::Associations::CollectionProxy)
            # Include name of collection in output
            out += "\n#{assoc.name.pluralize}:\n"
            out += self.send(a.name).to_a.inspect
          else
            out += self.send(a.name).inspect
          end
          out
        }).compact.join("\n  ")
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models in a has_many relationship such that Log has_many Items. Rails
I have two models, associated with a HABTM (actually using has_many :through on both
I have two models Customer and Transaction Customer has_many :transactions Transaction belongs_to :customer Now
I have two models: User and Car with the following associations: User has_many Car
I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models, Listing and Invitation, associated with has_and_belongs_to_many. I am looking at
I have two models that are associated with each other. Customer has_one :primary_contact And
I have two models: Program < ActiveRecord::Base has_many :events def federal_financing events.sum(&:federal_financing) end def
I have two models Company and Role related by has_many and belongs_to association respectively.
I have two models: Novel has_many :pages Page belongs_to :novel I want to list

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.