2014-07-23 92 views
0

我有一個任務,我沒有建立,但不再工作。我是新來的紅寶石軌道上,還沒有找到這個問題的解決方案呢。NoMethodError:未定義的方法`<<'爲零:NilClass

當我運行任務獲得:

這是任務:

​​

小調試通控制檯

確認有120530個負載轉移:

loads = Load.where("pickup < NOW()").count  
2014-07-23 12:55:56 DEBUG -- (149.8ms) SELECT COUNT(*) FROM "loads" WHERE (pickup < NOW()) 
=> 120530 

創建空arr AY:

expired_loads = [] 
=> [] 

運行where命令

loads = Load.where("pickup < NOW()") 

許許多多的記錄......我也僅限於兩個用於測試

1.9.3-p547 :006 > loads = Load.where("pickup < NOW()").limit(2) 
2014-07-23 13:02:07 DEBUG -- Load Load (74.2ms) SELECT "loads".* FROM "loads" WHERE (pickup < NOW()) LIMIT 2 
=> [#<Load id: 18398947, user_id: 11074, origin: #<RGeo::Geographic::SphericalPointImpl:0x595f056 "POINT (-80.48237609863281 37.772544860839844)">, dest: #<RGeo::Geographic::SphericalPointImpl:0x595ec64 "POINT (-78.30302429199219 40.295894622802734)">, length: 48, comments: "~PostEverywhere_20140721140916~", ltl: false, rate: nil, delivery: "2014-07-22 17:00:00", pickup: "2014-07-21 17:00:00", weight: 48, equipment_id: 8, covered: false, created_at: "2014-07-21 19:16:16", updated_at: "2014-07-21 19:16:16", owner: nil, deleted: false, origin_city: "ronceverte", origin_state: "wv", dest_city: "martinsburg", dest_state: "pa">, #<Load id: 18398948, user_id: 11074, origin: #<RGeo::Geographic::SphericalPointImpl:0x553cd2a "POINT (-81.035400390625 37.384891510009766)">, dest: #<RGeo::Geographic::SphericalPointImpl:0x553c9d8 "POINT (-79.80570983886719 40.317527770996094)">, length: 48, comments: "~PostEverywhere_20140721140916~", ltl: false, rate: nil, delivery: "2014-07-22 17:00:00", pickup: "2014-07-21 17:00:00", weight: 48, equipment_id: 8, covered: false, created_at: "2014-07-21 19:16:16", updated_at: "2014-07-21 19:16:16", owner: nil, deleted: false, origin_city: "princeton", origin_state: "wv", dest_city: "greenock", dest_state: "pa">] 

真的不知道該< <是什麼但我認爲這只是分佈的屬性,以便他們可以保存在另一個表

這裏

**NoMethodError: undefined method `<<' for nil:NilClass** 

下一行

loads.each {|load| expired_loads << load.attributes } 

錯誤是:將它們保存到archived_loads表和負載表中刪除

ArchivedLoad.create(expired_loads) 
loads.delete_all 

回答

0

爲什麼不使用#map呢? expired_loads = loads.map(&:attributes)或更好,但只需在Load中創建一個方法即可將其歸檔,例如

class Load 
    scope :expired_loads, -> {where("pickup < NOW()")} 
    def self.archive_now 
    ArchiveLoad.create(expired_loads.map(&:attributes)) 
    expired_loads.delete_all 
    end   
end 

然後就可以調用Loads.archive_now我不確定你的任何結構,這僅僅是一個建議,以更簡潔的方式。

如果您還在裏面使用了<<方法,也可以謹慎地發佈您的ArchivedLoadcreate方法。如果你還沒有實現某種自定義的create方法,我認爲你需要一個迭代,而不是如

expired_loads.map(&:attributes).each{|load| ArchiveLoad.create(load)} 

而且<<類似於push它添加一個對象的Array

結束
相關問題