32651:ERROR comparison of Float with Float failed ruby - ruby-on-rails

I'm new to rails, and I'm getting an error and I can't seem to find were the problem is. Here is the log:
[32651:ERROR] 2012-10-09 13:46:52 :: comparison of Float with Float failed
[32651:ERROR] 2012-10-09 13:46:52 :: /home/sunny/backend/lib/analytics/lifetime.rb:45:in `each'
/home/sunny/backend/lib/analytics/lifetime.rb:45:in `max'
/home/sunny/backend/lib/analytics/lifetime.rb:45:in `max_growth'
/home/sunny/backend/lib/analytics/lifetime.rb:15:in `run'
/home/sunny/backend/lib/analytics/post_analytics.rb:102:in `lifetime'
/home/sunny/backend/lib/analytics/post_analytics.rb:27:in `run`run'
lib/endpoints/posts/index.rb:65:in `block in response'
lib/endpoints/posts/index.rb:62:in `each'
lib/endpoints/posts/index.rb:62:in `response'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/api.rb:163:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/validation/required_param.rb:43:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/params.rb:61:in `block in call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/validator.rb:40:in `safely'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/params.rb:59:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/rack/async_middleware.rb:73:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/async-rack-0.5.1/lib/async_rack/async_callback.rb:114:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/async-rack-0.5.1/lib/async_rack/async_callback.rb:91:in `block in new'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/request.rb:163:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/goliath-1.0.0.beta.1/lib/goliath/request.rb:163:in `block in process'
Whenever I go to the file and find that row, there is no each, and I can't understand where the problem is. Can anyone help? More than a fix, I want to understand the problem.
Here is the file:
class Lifetime
attr_reader :values
GROWTH_CUT_OFF = 0.10
def initialize(engaged_users_values)
#values = engaged_users_values
end
def run
growths = growth(values)
return [0] if growths.uniq.first == 0 || growths.empty?
max_growth = max_growth(growths)
dead_growth = least_growth(growths, max_growth)
time_diff = time_diff(values.last['end_time'],
values.first['end_time'])
return [time_diff] unless dead_growth
dead_loc = growths.index(dead_growth)
dead_value = values[dead_loc]
lifetime(values.first, dead_value)
end
def time_diff(last, first)
in_minutes(parse_time(last)-parse_time(first))
end
def parse_time(time)
DateTime.parse(time)
end
def growth(values)
values.each_cons(2).map do |one, two|
one_value = one['value'].to_f
two_value = two['value']
((two_value-one_value)/(one_value)*100).round(2)
end
end
def max_growth(growths)
growths.max
end
def least_growth(growths, max_growth)
growth_cut = growth_cut(max_growth)
growths.select {|g| g <= growth_cut}.first
end
def growth_cut(max_growth)
max_growth*GROWTH_CUT_OFF
end
def lifetime(first_value, last_value)
first = parse_time(first_value['end_time'])
last = parse_time(last_value['end_time'])
result = last-first
[in_minutes(result)]
end
def in_minutes(time)
time.to_f*24*60
end
end

If one of both Floats is NaN then you get this message. So, prevent the NaN case.
We can give you more info if you post the exact code of the error (the max_growth method)

Related

Getting error undefined method `[]' for nil:NilClass (NoMethodError)

Here is the code
class Tech
attr_reader :content, :tech_hash
#tech_hash = Hash.new(0)
def initialize(content)
#content = content
showTech(content)
end
def showTech(content)
content.split.each do |word|
#tech_hash[word] += 1
end
#tech_hash = #tech_hash.sort_by{|k,v| -v}.to_h
p #tech_hash
end
end
class Digital
def analyze()
File.foreach('test.txt') do |content|
ob = Tech.new(content)
end
end
end
digi = Digital.new()
digi.analyze()
Here is the error
D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:10:in `block in showTech': undefined method `[]' for nil:NilClass (NoMethodError)
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:9:in `each'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:9:in `showTech'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:6:in `initialize'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:22:in `new'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:22:in `block in analyze'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:20:in `foreach'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:20:in `analyze'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:29:in `<main>'
#tech_hash is defined outside of an instance method, what makes it a class variable. Therefore within showTech there is still no #tech_hash instance variable defined and therefore it returns nil
Just move #tech_hash into the initialize method to initialize an instance variable:
def initialize(content)
#content = content
#tech_hash = Hash.new(0)
showTech(content)
end

Ruby : Getting error []': no implicit conversion of String into Integer (TypeError)

class LineAnalyzer
##highest_wf_count = 0
##highest_wf_words = Hash.new(0)
def initialize(content, line_number)
#content = content
#line_number = line_number
calculate_word_frequency(content)
end
def calculate_word_frequency(content)
content.split.each do |word|
##highest_wf_words[word.downcase] +=1
end
##highest_wf_words = ##highest_wf_words.sort_by{|k,v| -v}
end
end
class Solution
def initialize()
#analyzers = Array.new
#highest_count_words_across_lines = Array.new
end
def analyze_file()
#line_number = 0
File.foreach('test.txt') do |content|
#line_number += 1
#line = LineAnalyzer.new(content,#line_number)
#analyzers.<<(#line)
end
end
end
When trying to create LineAnalyzer object and passing values, getting this error in:
#line = LineAnalyzer.new(content,#line_number)
This is what I getting:
Max-Word-Freq/module2_assignment.rb:34:in `calculate_word_frequency'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/module2_assignment.rb:25:in `initialize'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/module2_assignment.rb:68:in `new'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/module2_assignment.rb:68:in `block in analyze_file'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/module2_assignment.rb:66:in `foreach'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/module2_assignment.rb:66:in `analyze_file'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/solution.rb:10:in `<main>'
You have this error because you are trying to use an Array like a Hash.
On this line :
##highest_wf_words = ##highest_wf_words.sort_by{|k,v| -v}
In the first time ##highest_wf_words is indeed a Hash but the sort_by method is returning an Array. So when you use it again you have an error.
If you want to sort your hash by value you can do this :
##highest_wf_words = Hash[##highest_wf_words.sort]
# Or reverse it
##highest_wf_words = Hash[##highest_wf_words.sort.reverse]
Look at this for further explanation : Hash::[]

sqlite3 to postgreSQL migration issues with Rails and ActiveRecord

Prior information: I have very little knowledge in Ruby on Rails and its Gems and Frameworks. So things which seem to be obvious may not be the case to me.
I need to migrate the database from sqlite3 to postgreSQL in our application containing AngularJS in the frontend and Ruby on Rails with ActiveRecord in the backend.
I installed the Ruby Gem pg and changed the database.yml as following:
# This was for sqlite
#adapter: sqlite3
#database: db/gfms.sqlite3
#pool: 5
#timeout: 5000
# This is for postgreSQL
adapter: postgresql
encoding: unicode
database: llqa
pool: 5
username: postgres
password: admin
host: localhost
port: 1234
timeout: 5000
schema_search_path: "gfms"
I'm having several issues now. Some queries return no records where they should, but don't generate an error. But:
A more specific problem I'm having is this error:
E, [2017-04-21T07:34:30.381466 #10961] ERROR -- : PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: ..."configtables" WHERE (parent IS NULL OR parent = "" OR pare...
^
: SELECT COUNT(*) FROM "configtables" WHERE (parent IS NULL OR parent = "" OR parent = 'model')
2017-04-21 07:34:30 - ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: ..."configtables" WHERE (parent IS NULL OR parent = "" OR pare...
^
: SELECT COUNT(*) FROM "configtables" WHERE (parent IS NULL OR parent = "" OR parent = 'model'):
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `block in exec_no_cache'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:378:in `block in log'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.16/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:372:in `log'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `exec_no_cache'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:954:in `select'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:265:in `execute_simple_calculation'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:227:in `perform_calculation'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:119:in `calculate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:34:in `count'
/Users/ndinatale/leanlogic-qa-prototype/server/lib/models_base.rb:135:in `get_list'
/Users/ndinatale/leanlogic-qa-prototype/server/routes/02_rest_misc.rb:266:in `block in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `block in compile!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `[]'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (3 levels) in route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:994:in `route_eval'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (2 levels) in route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1015:in `block in process_route'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `catch'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `process_route'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:973:in `block in route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `each'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1085:in `block in dispatch!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1082:in `dispatch!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `block in call!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `call!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:895:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:225:in `context'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:220:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/nulllogger.rb:9:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/head.rb:13:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/methodoverride.rb:22:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/show_exceptions.rb:25:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:182:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:2013:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `block in call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1785:in `synchronize'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1785:in `synchronize'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `call'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/configuration.rb:224:in `call'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/server.rb:600:in `handle_request'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/server.rb:435:in `process_client'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/server.rb:299:in `block in run'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/thread_pool.rb:120:in `call'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
The function where the error happens (the line is commented):
def get_list(limit = nil, page = nil)
coll = all
coll = (block_given?) ? (yield coll) : coll
rowcnt = coll.count # This is the line where the error happens
pages = (limit && limit > 0) ? (rowcnt.to_f/limit).ceil : 1
coll = coll.limit(limit) if limit && limit > 0
coll = coll.offset((page-1)*limit) if limit && limit > 0 && page && page > 1
ret = { rows: coll.all.to_a, rowcnt: rowcnt, page: page || 1, pages: pages, limit: limit || 0 }
def ret.serialize(include = nil)
self[:rows].collect! do |row|
hash = include ? (row.serializable_hash(include: include.incllist)) : row.serializable_hash
(hash['code'] && hash['code'].end_with?('(TR)')) ? nil : ((block_given?) ? yield(hash,row) : hash)
end
self[:rows].compact!
return self.rest_success,true,self
end
return ret
end
This is just an example. My question however is: Is there something in the configuration of ActiveRecord, Ruby or postgreSQL itself so it behaves equally like with sqlite3? What needs to be done, if anything?

I am using https://github.com/tyrauber/stock_quote to fetch stock data. However I am getting below mentioned error while doing so

I am new to ruby on rails. :(
while doing a search I am getting StockQuote::NoDataForStockError in StocksController#search ...........................
My model
class Stock < ActiveRecord::Base
def self.find_by_ticker(ticker_symbol)
where(ticker: ticker_symbol).first
end
def self.new_from_lookup(ticker_symbol)
looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
return nil unless looked_up_stock.name
new_stock = new(ticker: looked_up_stock, name: looked_up_stock.name)
new_stock.last_price = new_stock.price
new_stock
end
def price
closing_price = StockQuote::Stock.quote(ticker).close
return "#{closing_price} (closing)" if closing_price
opening_price = StockQuote::Stock.quote(ticker).open
return "#{opening_price (opening)}" if opening_price
"Unavailable"
end
end
Errors I am getting in console while doing a search.
StockQuote::NoDataForStockError: StockQuote::NoDataForStockError
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/stock_quote-1.2.3/lib/stock_q
uote/stock.rb:134:in `parse'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/stock_quote-1.2.3/lib/stock_q
uote/stock.rb:86:in `block in quote'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:228:in `call'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:228:in `process_result'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:178:in `block in transmit'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/net/http.rb:853:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:172:in `transmit'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:64:in `execute'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:33:in `execute'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/stock_quote-1.2.3/lib/stock_q
uote/stock.rb:84:in `quote'
from C:/Sites/tracker/app/models/stock.rb:19:in `price'
from C:/Sites/tracker/app/models/stock.rb:14:in `new_from_lookup'
from (irb):9
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/console.rb:110:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/console.rb:9:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/commands_tasks.rb:68:in `console'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
If you look at the code for that gem you can see that a StockQuote::NoDataForStockError is returned when the response code is not 200. You'll need to delve into what it doesnt like about the data you are providing. For example you should be able to query the response a bit more and at least determine what url is being sent.
The solution for page interrupting is to use begin rescue blocks every time when we call methods from StockQuote module. In begin part of block use regular call and in rescue part set value to nil and in that case return value 'Unavailable' and similar.begin rescue in Ruby is like try catch in Java. Example:
def price
begin
closing_price = StockQuote::Stock.quote(ticker).close
rescue
closing_price = nil
end
return "#{closing_price} (Closing)" if closing_price
begin
opening_price = StockQuote::Stock.quote(ticker).open
rescue
opening_price = nil
end
return "#{opening_price} (Opening)" if opening_price
'Unavailable'
end

NoMethodError: undefined method `delete' for nil:NilClass

i get this
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'
Match Model
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
User Model
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
I try to make a dota2 api.
When a user gives the match id, it gets the data from the api, and stores it in the db, so i don't need to bash their api
i think the problem is with
def initialize(response)
#response = response
getData(#response)
end
Change it to
def initialize(response)
super
#response = response
getData(#response)
end
According to this blog post Rails: Don't override initialize on ActiveRecord objects You shouldn't rely on this method for ActiveRecord objects.

Resources