Ruby on Rails Field Average

Calculate average of column value in Ruby on Rails

You should consider reading some documentation - obviously it's quick to get a answer on SO but sometimes the docs can lead you to something you didn't know to look for or ask for.

That said, the fastest way is to use average

profile.courses.average(:gpa)

This will give you an average. Or you can do it the long way, if for some reason you need make modifications in between.

profile.courses.sum(:gpa) / profile.courses.count

Ruby on Rails field average?

For your question, one could actually do:

@users.collect(&:score).sum.to_f/@users.length if @users.length > 0

Earlier I thought, @users.collect(&:score).average would have worked. For database fields, User.average(:score) will work. You can also add :conditions like other activerecord queries.

Rails calculate and display average

Together with other calculations, ActiveRecord provides a method called average. You can use it by implementing something like this:

class Movie
def self.average_by_genre(genre)
average(:rating).where(:genre => genre)
end
end

Alternatively, you can also implement it as an instance method of the Movie model.

class Movie
def average_rating
self.class.average(:rating).where(:genre => self.genre)
end
end

This would let you call the method directly from your view.

<article>
<header>
<h1><%= @movie.title %></h1>
</header>
<p>
<span>Average rating for genre <%= @movie.genre %>:</span>
<%= @movie.average_rating %>
</p>
</article>

How to average a column in a join table?

It happened because your 'value' field has a 'string' type. If value will use only as integer, you can change column type to integer or other numeric type, dependency by your database. If you wanna work with string, then if this values will be in integer range, your code can be like:
@item.items_ratings.where(rating_id: 1).average('CAST(value AS INT)')

Rails: how to calculate the average of a small set of elements

@medical.average(:max_salary) is expanded as @jobpostings.where(...).average(:max_salary).limit(4) even though the limit(4) appears previously in the method chain.

You can confirm this by checking the query that is run which be as follows:

SELECT  AVG(`jobpostings `.`max_salary `) FROM `tickets` ... LIMIT 4`

Effectively, LIMIT 4 doesn't do anything because there is only one average number in the result of the above query.

One way to accomplish what you are trying to do will be:

$ @top_salaries = @jobpostings.where("title like ? OR title like ?", "%MEDICAL SPECIALIST%", "%MEDICAL EXAMINER%").limit(4).map(&:max_salary)
=> [ 220480.0, 180000.0, 158080.0, 130000.0]
$ average = @top_salaries.reduce(:+) / @top_salaries.size
=> 172140.0

Extract average values of multiple columns via ActiveRecord

grades = Grade.where(student_id: 25, class_id: 123).pluck('avg(accuracy), avg(time)').first

Should get

[#<BigDecimal:7f9c5a890410,’8.0’,9(18)>, #<BigDecimal:7f9c5a8903c0,’10.0’,9(18)>]

and now convert them to simple floats.

grades.map!(&:to_f)

should get

[8.0, 10.0]

Rails & Active Record: Get the average value of a column after grouping

try this:

Keyword.joins(:user_keywords)
.select('keywords.name, avg(user_keywords.relevance_score) as score')
.group('keywords.name')
.order('score DESC')
.map(&:name)

RUBY ON RAILS 4.2.3 Calculate average

RESOLVE STEP BY STEP:

first step: Add to your model controller, to def show:
@rating = Model.where(:model_id => params[:id]).average(:attribute).to_i

second step: add to your model view to show.html.erb:
<%= @rating %>

model_id = your model, i.e. Post will be :post_id
:attribute = your attribute, i.e. :rating

It's all.



Related Topics



Leave a reply



Submit