(Object Doesn't Support #Inspect)

(Object Doesn't Support #Inspect)

I have a simple case, involving two model classes:

class Game < ActiveRecord::Base
  has_many :snapshots

  def initialize(params={})
   # ...
  end
end

class Snapshot < ActiveRecord::Base
  belongs_to :game

  def initialize(params={})
  # ...
  end
end

with these migrations:

class CreateGames < ActiveRecord::Migration
  def change
    create_table :games do |t|
      t.string :name
      t.string :difficulty
      t.string :status

      t.timestamps
    end
  end
end

class CreateSnapshots < ActiveRecord::Migration
  def change
    create_table :snapshots do |t|
      t.integer :game_id
      t.integer :branch_mark
      t.string  :previous_state
      t.integer :new_row
      t.integer :new_column
      t.integer :new_value

      t.timestamps
    end
  end
end

If I attempt to create a Snapshot instance in rails console, using

Snapshot.new

I get

(Object doesn't support #inspect)

Now for the good part. If I comment out the initialize method in snapshot.rb, then Snapshot.new works. Why is this happening?
BTW I am using Rails 3.1, and Ruby 1.9.2

1

12 Answers

This is happening because you override the initialize method of your base class (ActiveRecord::Base). Instance variables defined in your base class will not get initialized and #inspect will fail.

To fix this problem you need to call super in your sub class:

class Game < ActiveRecord::Base
  has_many :snapshots

  def initialize(params={})
   super(params)
   # ...
  end
end
1

I had this symptom when I had a serialize in a model like this;

serialize :column1, :column2

Needs to be like;

serialize :column1
serialize :column2
2

I ran into this issue when I used an invalid association name in a joins.

For example,

Book.joins(:authors).first

Should be

Book.joins(:author).first

Assuming a Book model belongs_to an Author model.

This can also happen when you implement after_initialize, particularly if you are attempting to access attributes which were not included in your select. For instance:

after_initialize do |pet|
  pet.speak_method ||= bark  # default
end

To fix, add a test for whether the attribute exists:

after_initialize do |pet|
  pet.speak_method ||= bark if pet.attributes.include? 'speak_method'  # default`
end

I'm not sure exactly why, but I got this error when I accidentally misspelled 'belongs_to' as 'belong_to' in the associated class definition.

I believe you forgot to

rails db:migrate

Try calling .valid? on the new object to see if you can get a more helpful error.

In my case, I got this error from a block of code that creates a new instance of one of my models and assigns values to its fields. It turns out that my code was assigning a value to one of the fields that Rails couldn't match with that field's type. Calling valid? on the new object gave me a more helpful error (undefined method `to_f' for #<MatchData...).

This is a misleading and nonspecific error. For instance, I just got it because I made a scope like this:

scope :posted, -> { where('posted_on_date <= ?', Date.today) }

when it should have been:

scope :posted, -> { where('post_on_date <= ?', Date.today) }

In my case, this was due to my mistakenly using the posted_on_date attribute.

I get this problem if the model contains an after_find.

The same error if you put the attribute type wrong:

attribute :publicar, :integer, default: true

instead of

attribute :publicar, :boolean, default: true

I was getting this error when running an ActiveRecord .where clause/method.

It was simply because there was a typo in the column name. Once I fixed the typo the query worked exactly as expected.

Wrong:

Package.where(scrape_nunber: 2)

Right (fixed typo in column name, and it works now):

Package.where(scrape_number: 2)

Just double check there isn't a typo in your column name(s) in the where clause.

I ran into this problem after trying to integrate devise authentication with an existing User model, I solved it by running command below:

spring stop

Don't know the exact cause but hope it helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.