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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:18:58+00:00 2026-05-22T15:18:58+00:00

I am defining @foo as a class instance attribute, and using the after_initialize callback

  • 0

I am defining @foo as a class instance attribute, and using the after_initialize callback to set the value of this when a record is created/loaded:

class Blog < ActiveRecord::Base
  @foo = nil

  after_initialize :assign_value

  def assign_value
    @foo = 'bar'
  end
end

However, when I inspect a Blog object, I am not seeing the @foo attribute:

 > Blog.first.inspect
=> "#<Blog id: 1, title: 'Test', created_at: nil, updated_at: nil>"

What do I need to do to get inspect to include this? Or conversely, how does inspect determine what to output?

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-22T15:18:59+00:00Added an answer on May 22, 2026 at 3:18 pm

    Active record determines which attributes to show in inspect based on the columns in the database table:

    def inspect
      attributes_as_nice_string = self.class.column_names.collect { |name|
        if has_attribute?(name)
          "#{name}: #{attribute_for_inspect(name)}"
        end
      }.compact.join(", ")
      "#<#{self.class} #{attributes_as_nice_string}>"
    end
    

    Lifted from base.rb on github

    To change the output of inspect you’ll have to overwrite it with your own method e.g.

    def inspect
      "#{super}, @foo = #{@foo}"
    end
    

    Which should output:

    > Blog.first.inspect
    => "#<Blog id: 1, title: 'Test', created_at: nil, updated_at: nil>, @foo = 'bar'"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
When defining a method on a class in Python, it looks something like this:
Given the following two ways of defining a class method in Ruby: class Foo
given this code: class Foo def bar return Bar.new end end class Bar ...
I want to define a set of constants in a class like: class Foo(object):
I am defining the following class: public class Foo<T> { T _value; public T
If I have an ActiveRecord model as follows class Foo < ActiveRecord::Base validates_inclusion_of :value,
Is there any shorthand way of defining and using generic definitions without having to
In Ruby, suppose I have a class Foo to allow me to catalogue my
Given an object foo of class Foo, I want to do the following: NSString

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.