0

結果モデルに、:measurement_id、:test_id、:player_id、:no、:resを含むいくつかのテストの結果があります。ここで、noはテストの数(たとえば、1-first、2-secondなど)であり、:resはresaultです。そのテストの。

今、私はこのようなことをするメソッドを書きたいと思います:

Result.where(:measurement_id = xx、:test_id = zz、:player_id = yy).finalresault

そしてfinalresaultは、テーブルにあるすべての:resを探して計算し、平均値を言ってそれを返します。

そのような方法で行うことは可能ですか?

ありがとうございました

これがモデルの様子で、私は何かを書き込もうとしていました...:

  class Result < ActiveRecord::Base
      attr_accessible :measurement_id, :test_id, :player_id, :no, :res
      belongs_to :test 

      def finalresault


      end  

    end
4

1 に答える 1

2

あなたが探しているのは次のようなものだと思います:

class Result < ActiveRecord::Base
  attr_accessible :measurement_id, :test_id, :player_id, :no, :res
  belongs_to :test 

  class << self
     def finalresault
       Result.where(:measurement_id=xx, :test_id=zz, :player_id=yy).each do |result|
         # tally up your result.res here (or whatever calculation you want)
       end
       # ...finish the calculation and return it here
     end
  end
end

インスタンスをインスタンス化せずに、クラスでこのメソッドを呼び出すことができます。

Result.finalresault
于 2012-05-04T01:10:12.290 に答える