awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

Object doesn't support #ai #339

Open dsfernandobond opened 6 years ago

dsfernandobond commented 6 years ago

Not sure if it's an awesome_print issue or something my end causing it but when trying display a certain objects fields I just receive the message: (Object doesn't support #ai), for example:

Quote.new (Object doesn't support #ai)

Any advice would be great thanks as I find awesome_print really useful.

mcfoton commented 5 years ago

It may be due to delegated method not being resolved, thus returning object is not an instance and awesome_print doesn't know how to handle it. Try doing object.valid? or object.relation.valid? to check what is returned

dsfernandobond commented 5 years ago

I'm not sure if it's that, all cases returning true

Nikolab8 commented 3 years ago

same issue here! @mcfoton is right (in my case) belongs_to/delegate to: ... same message as above. But, it work's just fine after i expelled gem from Gemfile, no error (ie. in console), unlike with awesome gem in Gemfile. (... nah, not exactly, just error is gone ...) .. So, got it! The issue indeed was wrong delegate (as @mcfoton said). Make sure to use right delegate syntax, which is to set it under using/destination Class, rather then originating (wierd?)... ie. class User is using delegated username from Profile, so in model user.rb write: Class User < ApplicationRecord delegate :username, to: :profile #not vice versa!

eric-norcross commented 2 years ago

In my case this is occurring with a non-databased backed model. When trying to access foo.errors, I get (Object doesn't support #ai) in console.

class Foo
  include ActiveModel::Model

  attr_accessor :bar

  def initialize(attrs = {})
    super

    @errors = ActiveModel::Errors.new(self)

    @bar = attrs[:bar].presence
  end

  validate :bar_exists

  def bar_exists
    errors.add(:bar, 'does not exist') unless @bar.present?
  end
end

Which yields:

f = Foo.new
f.valid? # => false
f.errors # => (Object doesn't support #ai)
f.errors.messages # =>
  {
    :bar => [
      [0] "does not exist"
    ]
  }

If I add an errors reader method and respond with errors.to_h it will respond with a hash and not the error, but that strips away all the other functionality of errors so it's not a suitable solution in this instance.