2015-04-07 64 views
0

我已在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 

,當我運行與選擇上有一個命令,我得到一個錯誤

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

是否有導致此特定設置?

+0

你正在使用哪個版本的Rails?我沒有看到任何問題 –

+0

我正在使用rails 4.0.2 –

+0

您是否在Tool類中定義了'initialize'方法? – Drenmi

回答

0

我發現了兩個很好的答案在下面的文章:

Object doesn't support #inspect

ActiveRecord after_initialize with select

重寫initialize在第一篇文章所示,然後丟棄after_initialize回調將解決您的問題。第二篇文章中提供了一個更爲優雅的解決問題的方法。問題的根源在於部分選擇與initialize方法之後的衝突。部分選擇模型時,執行after_initialize :defaults失敗,因爲defaults正在嘗試通過檢查variable.nil?來訪問未選中的列。如果記錄被保存,解決方法是隻調用after_initialize回調。

after_initialize :defaults, unless: :persisted? 

我有兩個after_initializeafter_find同樣的問題,我想情況是after_touch相同。

相關問題