yellowskull
yellowskull

Reputation: 177

BigDecimal undefined method

I'm getting an error when i'm trying to average out a column from search results:

undefined method `each' for #<BigDecimal:101780440,'0.1776030511 706048E4',27(45)>

View

<% @vone.each do |v| %>
<%= "%.2f" % (v.lint/227) %>
<% end %>

Controller

@vone = Result.where(params[:variety_one], params[:years]).includes(:trial).where(params[:region_id], params[:irrigated]).average('lint')

Upvotes: 1

Views: 3748

Answers (1)

andypp
andypp

Reputation: 249

The method 'average' returns number, hence it doesn't respond to 'each' method

http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-average

If you want to iterate through the result, then you should remove the average

@vone = Result.where(params[:variety_one], params[:years]).includes(:trial).where(params[:region_id], params[:irrigated])

Upvotes: 2

Related Questions