user2026178
user2026178

Reputation: 318

Using Descriptive Statistics gem on active record

I have installed descriptive statistics successfully and in attempting to use the methods provided (like Variance etc) , it keeps shooting out undefined method 'variance'. I thought the methods were just built in and could be used straight away.

Scores_controller.rb

def index                                         
      statsD = User.all.extend(DescriptiveStatistics)
      @var = Score.variance(&:strokes) 
  end

index.html.erb

Variance:  <%= @var %>

I'm attempting to apply the stats to my table full of scores. Please help.

Upvotes: 1

Views: 251

Answers (1)

MZaragoza
MZaragoza

Reputation: 10111

You have to extend DescriptiveStatistics for the model that you are trying to use. It looks like you are extending User but want to use Score

def index                                         
  scores = Score.all.extend(DescriptiveStatistics)
  @variance = scores.variance(&:strokes) 
end

now in your view do

Variance:  <%= @variance %>

take a look at this example app https://github.com/mzaragoza/sample_descriptive_statistics

Upvotes: 1

Related Questions