是什么导致对象(模型)不支持检查?

时间:2015-04-07 07:16:04

标签: ruby-on-rails postgresql activerecord

我在SO中搜索了对象并不支持检查错误

,部分链接包括this one herethis one too 。还有更多,在github上,但他们都没有解决原因。

我的模特在这里

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

当我运行带有select的命令时,我收到错误

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

是否有导致此问题的特定设置?

1 个答案:

答案 0 :(得分:0)

我在以下帖子中找到了两个好的答案:

Object doesn't support #inspect

ActiveRecord after_initialize with select

重写initialize,如第一篇文章所示,然后删除after_initialize回调将解决您的问题。在第二篇文章中找到了问题根源的更优雅的解决方案。问题的根源是部分选择与您的after initialize方法冲突。部分选择模型时,after_initialize :defaults执行defaults失败,因为variable.nil?正在尝试通过选中after_initialize来访问未选择的列。解决方案是仅在记录持久化时调用after_initialize :defaults, unless: :persisted? 回调。

after_initialize

我在after_findafter_touch都遇到了同样的问题,我想SELECT a.id, b.b_pids, c.c_pids, d.d_ids FROM a_table a LEFT JOIN (SELECT b.customer_account_guid, GROUP_CONCAT(b.product_id SEPARATOR ':') as b_pids FROM b_table b GROUP BY b.customer_account_guid ) b ON a.customer_account_guid = b.customer_account_guid LEFT JOIN (SELECT c.customer_account_guid, GROUP_CONCAT(c.product_id SEPARATOR ':') as c_pids FROM c_table c GROUP BY b.customer_account_guid ) c ON a.customer_account_guid = c.customer_account_guid LEFT JOIN (SELECT d.customer_account_guid, GROUP_CONCAT(d.product_id SEPARATOR ':') as d_pids FROM d_table d GROUP BY d.customer_account_guid ) d ON a.customer_account_guid = d.customer_account_guid WHERE a.category = 'product_category' AND a.location = 'UK'; 上的情况相同。