explainer
explainer

Reputation: 1335

(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

Upvotes: 14

Views: 40666

Answers (12)

JayantSeth
JayantSeth

Reputation: 408

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.

Upvotes: 0

stevec
stevec

Reputation: 52638

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.

Upvotes: 0

jesus.barbero
jesus.barbero

Reputation: 111

The same error if you put the attribute type wrong:

attribute :publicar, :integer, default: true

instead of

attribute :publicar, :boolean, default: true

Upvotes: 0

haroldus
haroldus

Reputation: 131

I get this problem if the model contains an after_find.

Upvotes: 0

therealrodk
therealrodk

Reputation: 444

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.

Upvotes: 0

Marcel Kooi
Marcel Kooi

Reputation: 405

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.

Upvotes: 7

Khan
Khan

Reputation: 81

I believe you forgot to

rails db:migrate

Upvotes: 1

rockusbacchus
rockusbacchus

Reputation: 1347

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...).

Upvotes: 0

Mark Schneider
Mark Schneider

Reputation: 380

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

Upvotes: 3

Troy Anderson
Troy Anderson

Reputation: 606

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

Upvotes: 7

Jan Hettich
Jan Hettich

Reputation: 9886

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.

Upvotes: 1

halfdan
halfdan

Reputation: 34234

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

Upvotes: 10

Related Questions