2017-09-21 8 views
0
I have this code: 

    report = report.Where(....) 
    money = get_money report 

def get_money(report, count) 
      price= Report 
       .where('Month(date) =? and Year(date) =? ', 
        report.date.month, report.date.year).first 


      money= price.presence ? price.value.to_f/count: hardcoded_default__value 
      return royalty_value 
end 

このget_money関数をレポートモデルに移動するにはどうすればよいですか?関数をモデルレールに移動する

class Report < ActiveRecord::Base 
+0

あなたのコードにエラーがあるので、それはあなたの意図はここに何があるかは明らかではありません。 '#get_money'の目的は何で、' report'オブジェクトの中に 'price'があり、' Report'は '#value'メソッドを持っていますか? – EJ2015

答えて

0

アプリ/モデル/ report.rbにget_moneyメソッドを追加します。

class Report < ActiveRecord::Base 
    def get_money(count) 
    price = Report.where('Month(date) =? and Year(date) =? ', 
      report.date.month, report.date.year).first 
      money= price.presence ? price.value.to_f/count: hardcoded_default__value 
    return royalty_value 
    end 
end 

だけでなく、その変数が定義されていないので、方法(あなたがroyalty_value追加する必要があるとしている)をget_money機能)。

次に、あなたのレポートにインスタンスメソッドを呼び出すことができます。

report = report.Where(....).take 
money = report.get_money(count) # assuming the count variable is set 
関連する問題