ruby-on-railsruby-on-rails-7

after upgrading to rails 7, active record objects no longer show attributes?


When I was on rails 6, when loading a record from the database in the rails console, I would see:

MyFancyModel.last
=> #<MyFancyModel:0x0000000abc123
 id: 123,
 name: 'my name',
 some_column: 'foo'
 some_other_column: 'bar',
 ...etc>

Now after upgrading to rails 7, when I do this, I only see the id column:

MyFancyModel.last
=> #<MyFancyModel:0x0000000abc123 id: 123>

And I have to manually call .attributes on the object to see them...

I was looking through this guide: https://guides.rubyonrails.org/configuring.html

But do not see anything in there for config.active_record that looks like it would cause this behavior...


Solution

  • Your issue is very version specific. This Pull Request https://github.com/rails/rails/pull/49765 introduced a change to ActiveRecord::Core#inspect in Rails 7.2.0.

    The implementation allowed for the configuration of the attributes that would be shown during inspection using a class attribute called attributes_for_inspect. This was defaulted to just :id.

    In 7.2.2 the default was changed to :all which will display all attributes as it used to while allowing for the configuration on a per model basis.

    Although in production (for new applications) this is still set to [:id] at a configuration level.

    You can find both changes in the CHANGELOG

    You can configure this as desired by setting

    config.active_record.attributes_for_inspect = :all