0

I have searched about in SO for Object doesn't not support inspect error

and some of the links include this one here and this one too . There many more, and on github as well, but they all do not address the cause.

I have my model here

class Tool < ActiveRecord::Base
  include PublicActivity::Common
  after_initialize :defaults
  after_save       :explicit_updates
  belongs_to :job
  belongs_to :kind
  has_one    :location, :through => :job
  has_one    :rig, :through => :job
  belongs_to :vendor  
  has_paper_trail

  scope :deployable_by_type, ->(kind_id) { where(status: "deployable", 
                                         kind_id: kind_id)}
  scope :deployable, -> { where(status: "deployable")}
  scope :deployed, -> { where(status: "deployed")}
  scope :in_transit, -> { where(status: "in_transit")}
  scope :under_maintenace, -> { where(maintenance_status: true)}
  scope :failed, -> { where(failed: true)}
  scope :requiring_service, -> { where(service_required: true,      
                                maintenance_status: false)}


  validates  :kind_id,                presence: true
  validates  :serial_number,          presence: true
  validates  :department,             presence: true
  validates  :size,                   presence: true, numericality: { 
                                                :greater_than => 0}
  validates  :description,            presence: true
  validates  :hours,                  presence: true, 
                                      numericality: { 
                                       :greater_than_or_equal_to => 0 } 
  validates  :length,                 presence: true, numericality: { 
                                       :greater_than => 0 }
  validates  :vendor_id,              presence: true
  validates  :cost,                   presence: true
  validates  :service_hours,          presence:true, numericality: {    
                                       :greater_than => 0}    
  validates  :sensor_type,            presence: true

  def defaults
    self.status ||= "deployable"
    self.hours  ||= 0
    self.failed ||= false
    self.service_required ||= false
    self.maintenance_status ||= false
    self.daily_job_monitor ||=false
  end

  def explicit_updates
    if !self.failed
      self.update_columns(failure_comment: nil)
    end
  end
end

and When I run a command with select on it, I get an error

irb(main):002:0> Tool.select(:kind_id)
  Tool Load (0.8ms)  SELECT kind_id FROM "tools"
(Object doesn't support #inspect)

Is there a specific setting that is causing this?

Community
  • 1
  • 1
Leonard Kakande
  • 675
  • 1
  • 10
  • 17

1 Answers1

0

I found two good answers in following posts:

Object doesn't support #inspect

ActiveRecord after_initialize with select

Rewriting initialize as shown on the first post and then dropping the after_initialize callback will solve your problem. A more elegant solution at the source of the problem is found on the second post. The source of the problem is partial select conflicting with your after initialize method. When partially selecting the model fails executing after_initialize :defaults as defaults is trying to access unselected columns by checking variable.nil?. The solution is to only invoke the after_initialize callback if the record is persisted.

after_initialize :defaults, unless: :persisted?

I had the same issue on both after_initialize and after_find, and I guess the case is identical on after_touch.

Community
  • 1
  • 1
Mr. Jaeger
  • 105
  • 1
  • 6