Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(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

like image 247
explainer Avatar asked Oct 07 '11 17:10

explainer


People also ask

What is object doesn't support this property or method?

Reasons for the Run-time error 438: “Object doesn't support this Property or Method” This error occurs when you are trying to use a method/property that the specified object does not support (i.e. it is not found in the list of available methods or properties of the object).

How do I fix error 438?

Step one to fixing the 438 error is to uninstall the Microsoft Works add-in for Workplace. This usually causes battle with the Workplace software program. This may be performed by clicking on Begin > Management Panel > Add / Take away Packages, after which finding the “Microsoft Works” add-in.

What is Run Time error 91 in VBA?

Error: "Runtime Error 91" is a Visual BASIC error which means "Object variable not set". This indicates that the object was never created using the "Set" command before being used.

What is run time error 424 in VBA?

Object name used in script does not match the object name on the picture. Rename the object on the picture to match the name of the object in the VBA script. A mismatch of an object name between a picture and its script is one reason for the "Run-time error '424'".


1 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
like image 162
halfdan Avatar answered Sep 18 '22 22:09

halfdan