(Object doesn't support #inspect)
Asked Answered
P

12

14

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

Phillane answered 7/10, 2011 at 17:23 Comment(1)
Although it may not have been your issue, this comes up when there's an error raised in a custom inspect method. The original error is not visible, which can be annoying.Dermatoglyphics
F
10

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
Flimsy answered 9/11, 2011 at 23:15 Comment(1)
Why are you passing params to super? What would ActiveRecord::Base do with it?Coggins
S
8

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
Shayla answered 3/7, 2013 at 14:48 Comment(2)
and I wrote serialize :description, Array inproperly (as serialize :description, :array)Photogravure
I had a similar issue when I was improperly serializing a jsonb column using HashSerializer with calls to_json. I managed to put string JSON into the jsonb column, which messed up Rails reading the data back out.Bonanno
J
7

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.

Joris answered 21/5, 2022 at 20:25 Comment(0)
J
3

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
Javelin answered 4/6, 2019 at 5:47 Comment(0)
N
1

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.

Nelle answered 22/9, 2012 at 17:18 Comment(0)
C
1

I believe you forgot to

rails db:migrate
Campbellite answered 31/3, 2022 at 21:22 Comment(0)
V
0

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

Villada answered 24/12, 2020 at 15:44 Comment(0)
N
0

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.

Nucleotide answered 12/5, 2021 at 16:27 Comment(0)
C
0

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.

Cheeseparing answered 5/8, 2022 at 22:11 Comment(0)
T
0

I get this problem if the model contains an after_find.

Templia answered 2/9, 2022 at 6:2 Comment(0)
R
0

The same error if you put the attribute type wrong:

attribute :publicar, :integer, default: true

instead of

attribute :publicar, :boolean, default: true
Redfin answered 16/11, 2022 at 13:58 Comment(0)
W
0

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.

Wrung answered 21/2, 2023 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.