简体   繁体   中英

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

I have a task that I did not build but is no longer working. i am new to ruby on rails and have not found a solution for this problem yet.

when i run the task i Get:

this is the task:

  desc "Archive Loads/Trucks that have expired."
  task :expire_old_posts => :environment do
    expired_loads = []
    loads = Load.where("pickup < NOW()")
    loads.each {|load| expired_loads << load.attributes } 
    ArchivedLoad.create( expired_loads )
    loads.delete_all
  end

little Debugging thru console

Confirms there are 120530 loads to transfer:

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

create the empty array:

expired_loads = []
=> []

run the where command

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

lots and lots of records... i limited it to two for testing

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">] 

Really not sure what the << is but i assume this is just distributing the attributes so that they can be saved in the other table

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

Errors here with

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

Next line is: Saves them to archived_loads table and deletes from loads table

ArchivedLoad.create( expired_loads )
loads.delete_all

Why not use #map instead? expired_loads = loads.map(&:attributes) or better yet just create a method in Load to archive them eg

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

Then you can call Loads.archive_now I am unsure of any of your structure this is just a suggestion as to a more concise implementation.

It also might be prudent to post the create method of your ArchivedLoad in case you have utilized the << method inside of there as well. If you have not implemented some kind of a custom create method I think you need a iteration instead such as

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

Also << is similar to push it adds an object to the end of an Array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM