ActionView::Template::Error (can't convert Symbol into Integer) - ruby-on-rails

Doing one iteration with a hash in ruby, but some times in production(only on production) getting this symbol error.
Controller
#d1 = Model.get_driver_details
Model
def get_driver_details
driver_det = Hash.new
driver_det[:driver_details] = Table.select('name as d_name, SUM(total) as
total_count').group('driver.id')
end
Result
{:driver_details=>[{:d_name=>"Tomy", :total_count=>"25"}]}
Iteration
total_count = 0
#d1[:driver_details].each do |driver|
total_count += driver[:total_count].to_f
end
So i am getting this error ActionView::Template::Error (can't convert Symbol into Integer) in this line #d1[:driver_details].each do |driver|

This method is not returning the driver_det hash, it's only returning one entry in the hash and that entry is an array. (so it expects an integer for indexing, hence the error)
def get_driver_details
driver_det = Hash.new
driver_det[:driver_details] = Table.select('name as d_name, SUM(total) as
total_count').group('driver.id')
end
If you return the hash (reference it in the last line), you'll be ok.
def get_driver_details
driver_det = Hash.new
driver_det[:driver_details] = Table.select('name as d_name, SUM(total) as
total_count').group('driver.id')
driver_det
end
I assume you plan to support other keys in future?

Related

array.select not iterating through every element

I have a rails controller and this code only loop through the first element in the metrics array? Why is that?
# /metrics/:id
def values
#metric = metrics.select do |metric|
id = metric['href'].split('/').last
p "id == params[:id] = #{id == params[:id]}" # false on the first iteration (but never gets to the next iteration
return id == params[:id]
end
p "HERE?" # We never get here!
end
You need to remove the return statement from your method, Ruby uses implicit return (see https://jtrudell.github.io/blog/ruby_return_values/), so the result of a block is the last line that is evaluated in that block, the return statement in your code is treated as a return from the values method. Your method needs to look something like:
def values
#metric = metrics.select do |metric|
metric['href'].split('/').last == params[:id]
end
end

properly access key and value to handle multiple records

The following
#bucketitems = Bucketitem.group(:p_id).having('count("p_id") > 1').count(:p_id)
generates a hash of cases
{"00000450155"=>3, "00002860120"=>2, "00002870129"=>2, [...]}
If #bucketitems.first called, an array is given ["00000450155", 3]
How can each case now be invoked
#bucketitems.each do |key, value|
#items = Bucketitem.where('p_id = ?', "00000450155").to_a
so that the resulting array can be processed (complete missing data, delete duplicate records...)?
#items = Bucketitem.where('p_id = ?', key).to_a
is returning nil...

How to use the return values of this function?

i have a function:
def get_vals
#do something...
#
#
#
return arr1, arr2, arr3
end
arr1, arr2, arr3 are arrays.
Now I want to use these in my action:
def juko
results = article.get_vals
puts results
#
end
With the puts command, I checked, that i get the 3 Arrays back. But I can not access it.
I tried it so:
#data_array = results[:arr1]
#data_input = results[:arr2]
#pairs = results[:arr3]
I get the message: "no implicit conversion of symbol into integer"
Can anybody help me?
get_vals returns you an array.
Hotfix:
#data_array, #data_input, #pairs = results
Or, cleaner approach (return a hash from get_vals method):
def get_vals
#
#
#
{ data_array: arr1, data_input: arr2, pairs: arr3 }
end
Now:
#data_array = results[:data_array]
#data_input = results[:data_input]
#pairs = results[:pairs]

TypeError: no implicit conversion of Symbol into Integer

I encounter a strange problem when trying to alter values from a Hash. I have the following setup:
myHash = {
company_name:"MyCompany",
street:"Mainstreet",
postcode:"1234",
city:"MyCity",
free_seats:"3"
}
def cleanup string
string.titleize
end
def format
output = Hash.new
myHash.each do |item|
item[:company_name] = cleanup(item[:company_name])
item[:street] = cleanup(item[:street])
output << item
end
end
When I execute this code I get: "TypeError: no implicit conversion of Symbol into Integer" although the output of item[:company_name] is the expected string. What am I doing wrong?
Your item variable holds Array instance (in [hash_key, hash_value] format), so it doesn't expect Symbol in [] method.
This is how you could do it using Hash#each:
def format(hash)
output = Hash.new
hash.each do |key, value|
output[key] = cleanup(value)
end
output
end
or, without this:
def format(hash)
output = hash.dup
output[:company_name] = cleanup(output[:company_name])
output[:street] = cleanup(output[:street])
output
end
This error shows up when you are treating an array or string as a Hash. In this line myHash.each do |item| you are assigning item to a two-element array [key, value], so item[:symbol] throws an error.
You probably meant this:
require 'active_support/core_ext' # for titleize
myHash = {company_name:"MyCompany", street:"Mainstreet", postcode:"1234", city:"MyCity", free_seats:"3"}
def cleanup string
string.titleize
end
def format(hash)
output = {}
output[:company_name] = cleanup(hash[:company_name])
output[:street] = cleanup(hash[:street])
output
end
format(myHash) # => {:company_name=>"My Company", :street=>"Mainstreet"}
Please read documentation on Hash#each
myHash.each{|item|..} is returning you array object for item iterative variable like the following :--
[:company_name, "MyCompany"]
[:street, "Mainstreet"]
[:postcode, "1234"]
[:city, "MyCity"]
[:free_seats, "3"]
You should do this:--
def format
output = Hash.new
myHash.each do |k, v|
output[k] = cleanup(v)
end
output
end
Ive come across this many times in my work, an easy work around that I found is to ask if the array element is a Hash by class.
if i.class == Hash
notation like i[:label] will work in this block and not throw that error
end

Rails Fixnum Error

I have a simple query that Rails seems to be interpreting as a fixnum, but I'm not sure why. My code looks like this:
#user_with_points = Point.select("sum(points) as points, user_id").order("points desc").group("user_id")
#user_with_points.each_with_index do |user_with_point, index|
When I add puts #user_with_points, it shows:
#<Point:0x6360138>
#<Point:0x6322f38>
However, I'm receiving this error this error:
NoMethodError: undefined method 'each' for 75:Fixnum
adding Entire Code
def self.update_overall_rank_and_points
#user_with_points = Point.select("sum(points) as points, user_id").order("points desc").group("user_id")
rank = 0
points = 0
#user_with_points.each_with_index do |user_with_point, index|
#user = User.find(user_with_point.user_id)
if user_with_point.points != points
points = user_with_point.points
rank += 1
end
#user.rank = rank
#user.points = user_with_point.points
#user.save
end
end
Your query is returning a scalar value which the sum of points as an integer. The total of your query happens to be 75, hence the error. Therefore you can't do an each against it since it's not an enumeration.
Try:
#user_with_points = Point.sum(:points, :group => :user_id, :order => 'sum(points)')
#user_with_points.each do |user_id, points|
#...
user = User.find(user_id)
if user.points != points
puts "not equal!"
end
end

Resources