NoMethodError: undefined method `delete' for nil:NilClass - ruby-on-rails

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.

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

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

Rails undefined method update_attributes

I have the following migration file ...
... the goal is to change several name attributes in the database ...
class UpdateActionableItemName < ActiveRecord::Migration
class InsightReportMenuItem < ActiveRecord::Base
self.table_name = 'actionable_items'
attr_accessible :name
end
def up
#find all the rows to be updated
prescriber_activity_request = InsightReportMenuItem.where(name: "Prescriber Activity With Patient")
dispenser_activity_request = InsightReportMenuItem.where(name: "Dispenser Activity")
patient_history_request = InsightReportMenuItem.where(name: "Patient Request Activity")
debugger
#update row attributes
prescriber_activity_request.update_attributes(:name, "Prescriber Activity Request") if prescriber_activity_request
dispenser_activity_request.update_attributes(:name, "Dispenser Activity Request") if dispenser_activity_request
patient_history_request.update_attributes(:name, "Patient History Request") if patient_history_request
#save updates
prescriber_activity_request.save!
dispenser_activity_request.save!
patient_history_request.save!
end
def down
#find all the rows to be updated
prescriber_activity_request = InsightReportMenuItem.where(name: "Prescriber Activity Request")
dispenser_activity_request = InsightReportMenuItem.where(name: "Dispenser Activity Request")
patient_history_request = InsightReportMenuItem.where(name: "Patient History Request")
#update row attributes
prescriber_activity_request.update_attributes(:name, "Prescriber Activity With Request") if prescriber_activity_request
dispenser_activity_request.update_attributes(:name, "Dispenser Activity") if dispenser_activity_request
patient_history_request.update_attributes(:name, "Patient Request Activity") if patient_history_request
#save updates
prescriber_activity_request.save!
dispenser_activity_request.save!
patient_history_request.save!
end
end
... but when I run rake db:migration I get an error ...
... what's wrong here?
I've tried several different versions of syntax and I'm still getting the same bounce back error.
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `update_attributes' for #<ActiveRecord::Relation:0x0000010e2e4d20>/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/relation/delegation.rb:45:in `method_missing'
/Users/kweihe/pmpaware-webapp/db/migrate/20150706132233_update_actionable_item_name.rb:15:in `up'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:389:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:528:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `call'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/transactions.rb:208:in `transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `ddl_transaction'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:719:in `block in migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `each'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:570:in `up'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:551:in `migrate'
/Users/kweihe/.rvm/gems/ruby-2.1.1/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
UPDATE: SOLVED
added .first to the end of the query
prescriber_activity_request = InsightReportMenuItem.where(name: "Prescriber Activity With Patient").first
dispenser_activity_request = InsightReportMenuItem.where(name: "Dispenser Activity").first
patient_history_request = InsightReportMenuItem.where(name: "Patient Request Activity").first
update_attributes should be called on a single instance.
Your variables, such as prescriber_activity_request are relations, which are a wrapper around an SQL query. Try changing it to eg
prescriber_activity_request.update_all(:name, "Prescriber Activity With Request") if prescriber_activity_request.size > 0

ruby on rails scopes - ArgumentError: wrong number of arguments(1 for 0)

I'm getting a wrong arguments error when invoking a scope:
Report.start_driver_time(Unit.find(3007),Driver.find(2),3,2013)
scope :start_driver_time, lambda { |unit, driver, month, year|
where("unit_id = ? AND
driver_id = ? AND
extract(MONTH FROM time) = ? AND
extract(YEAR FROM time) = ?",
unit.id, driver.id, month, year)
.order("time asc")
.min(:time)
}
#select(:time) select([:id, :time])
scope :next_driver_time, lambda {|unit, time|
joins(:alerts => {:alert_code => :alert_unit_codes})
.where("reports.unit_id = ? AND
reports.time >= ? AND
alert_unit_codes.name = ?",
unit.id, time, "Seat Belt OFF")
.min(:time)
}
scope :reports_within_driving_period, lambda { |start_time, end_time|
where("unit_id = ? AND
reports.time >= ? AND
reports.time <= ?",
start_time, end_time)
}
stacktrace:
ArgumentError: wrong number of arguments(1 for 0)
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation/delegation.rb:37:in `min'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation/delegation.rb:37:in `method_missing'
from /Users/johnmerlino/Documents/github/XKTrackingSystem/app/models/report.rb:40:in `block in <class:Report>'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/named.rb:180:in `call'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/named.rb:180:in `block (2 levels) in scope'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/default.rb:41:in `block in unscoped'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation.rb:241:in `block in scoping'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping.rb:98:in `with_scope'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation.rb:241:in `scoping'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/default.rb:41:in `unscoped'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/named.rb:180:in `block in scope'
from (irb):1
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p0 :002 > exit
I think you intend to use minimum instead of min? I haven't found a reference to min, so it might be using a different method that uses no parameters.

32651:ERROR comparison of Float with Float failed ruby

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)

Resources