2013-12-15 117 views
0

我得到這個NoMethodError:未定義的方法`刪除」的零:NilClass

irb(main):001:0> Match.create(id: 401381886) 
    (0.0ms) begin transaction 
    Match Exists (1.0ms) SELECT 1 AS one FROM "matches" WHERE "matches"."id" = 401381886 LI 
MIT 1 
    User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 4294967295 
    User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 105382029 
    (0.0ms) rollback transaction 
NoMethodError: undefined method `delete' for nil:NilClass 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/attribute_methods/write.rb:33:in `write_attribute' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/attribute_methods/dirty.rb:70:in `write_attribute' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/attribute_methods/write.rb:19:in `__temp__57375627f59646=' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/attribute_methods.rb:126:in `method_missing' 
     from W:/projects/rails/private/dota2monster/app/models/match_performance.rb:13:in 
`block in getData' 
     from W:/projects/rails/private/dota2monster/app/models/match_performance.rb:11:in 
`each' 
     from W:/projects/rails/private/dota2monster/app/models/match_performance.rb:11:in 
`getData' 
     from W:/projects/rails/private/dota2monster/app/models/match_performance.rb:7:in ` 
initialize' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/inheritance.rb:27:in `new' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/inheritance.rb:27:in `new' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/persistence.rb:36:in `create' 
     from W:/projects/rails/private/dota2monster/app/models/match.rb:25:in `getData' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/ 
active_support/callbacks.rb:387:in `_run__512386826__save__callbacks' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/ 
active_support/callbacks.rb:80:in `run_callbacks' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/callbacks.rb:299:in `create_or_update' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/persistence.rb:106:in `save' 
... 3 levels... 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/transactions.rb:326:in `block in with_transaction_returning_status' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/connection_adapters/abstract/database_statements.rb:202:in `block in transact 
ion' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transa 
ction' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/connection_adapters/abstract/database_statements.rb:202:in `transaction' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/transactions.rb:209:in `transaction' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/transactions.rb:323:in `with_transaction_returning_status' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/transactions.rb:270:in `block in save' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/transactions.rb:281:in `rollback_active_record_state!' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/transactions.rb:269:in `save' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/a 
ctive_record/persistence.rb:37:in `create' 
     from (irb):1 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails 
/commands/console.rb:90:in `start' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails 
/commands/console.rb:9:in `start' 
     from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails 
/commands.rb:64:in `<top (required)>' 
     from bin/rails:4:in `require' 

匹配型號

class Match < ActiveRecord::Base 
    include HTTParty 

    has_many :matchperformances 
    has_many :users, through: :matchperformances 

    before_save :getData 

    validates :id, presence: true, uniqueness: true 
    def response(api_id) 
     base_url = "http://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?key=5F45B214200C4274114FE87E3A62E7B8&match_id=" 
     url = base_url + api_id.to_s 
     response = HTTParty.get(url) 
    end 
    def getData 
     response = response(self.id) 
     match = response["result"] 
     self.radiant_win = match["radiant_win"] 
     self.duration = match["duration"] 
     self.start_time = match["start_time"] 
     self.lobby_type = match["lobby_type"] 
     self.human_players = match["human_players"] 
     self.leagueid = match["leagueid"] 
     self.game_mode = match["game_mode"] 
     MatchPerformance.create(response) 
    end 
end 

用戶模型

class User < ActiveRecord::Base 
    has_many :match_performances 
    has_many :matches, through: :match_performances 
end 

MatchPerformance型號

class MatchPerformance < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :match 

    def initialize(response) 
     @response = response 
     getData(@response) 
    end 

    def getData(response) 
     response["result"]["players"].each do |player| 
      unless User.where(id: player["account_id"]).blank?    
       self.user_id = player["account_id"] 
       self.match_id = response["result"]["match_id"] 
       self.player_slot = player["player_slot"] 
       self.hero_id = player["hero_id"] 
       self.item_0 = player["item_0"] 
       self.item_1 = player["item_1"] 
       self.item_2 = player["item_2"] 
       self.item_3 = player["item_3"] 
       self.item_4 = player["item_4"] 
       self.item_5 = player["item_5"] 
       self.kills = player["kills"] 
       self.deaths = player["deaths"] 
       self.assits = player["assits"] 
       self.leaver_status = player["leaver_status"] 
       self.gold = player["gold"] 
       self.last_hits = player["last_hits"] 
       self.denies = player["denies"] 
       self.gold_per_min = player["gold_per_min"] 
      end 
     end 
    end 
end 

我試着做一個dota2 api。 當用戶給出匹配ID,它就會從API DB中的數據,並將其存儲,所以我不需要來砸他們的API

我認爲這個問題是

def initialize(response) 
     @response = response 
     getData(@response) 
    end 
+0

你應該提供更多的堆棧跟蹤 - 至少在它指向你的代碼的地方,然後確保你包含有問題的代碼。你基本上是問爲什麼你的代碼產生這個錯誤,但沒有提供通話記錄。 –

+0

這可能不會有太大的幫助,但是你失敗了,因爲'@ attributes_cache'在ActiveRecord中是'nil',它不應該是。 Google「activerecord @attributes_cache nil」顯示了一些類似的問題,但不幸的是沒有明顯的答案。 –

回答

相關問題