NoMethodError:nil的未定义方法`delete':NilClass

时间:2013-12-15 15:19:27

标签: ruby-on-rails api

我明白了

    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 Model

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

1 个答案:

答案 0 :(得分:0)

将其更改为

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

根据此博客文章Rails: Don't override initialize on ActiveRecord objects您不应该依赖此方法来处理ActiveRecord对象。