Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object doesn't support #ai #339

Open
dsfernandobond opened this issue May 3, 2018 · 4 comments
Open

Object doesn't support #ai #339

dsfernandobond opened this issue May 3, 2018 · 4 comments

Comments

@dsfernandobond
Copy link

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
Copy link

mcfoton commented Mar 21, 2019

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
Copy link
Author

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

@Nikolab8
Copy link

Nikolab8 commented Mar 3, 2021

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
Copy link

eric-norcross commented Mar 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants