モデルの属性を出力して、さまざまなモデルを反復処理するメソッドがあります。
def self.get_attributes(options = {})
options[:model_name].find(:all, :conditions => {:generated_at => options[:start_date]..options[:end_date]}).each do |p|
puts p.options[:col_name]
end
メソッドを呼び出すには:
start_date = DateTime.strptime(params[:start_date], '%m/%d/%Y').beginning_of_day.strftime("%Y-%m-%d %H:%M:%S")
end_date = DateTime.strptime(params[:end_date], '%m/%d/%Y').end_of_day.strftime("%Y-%m-%d %H:%M:%S")
get_attributes({:model_name => Revenue, :col_name => "revenue", :start_date => start_date, :end_date => end_date})
列名を文字列と変数として渡そうとしました。変数として渡されると、undefined variable
例外が発生します。上記のように、undefined method 'col_name'
例外が発生します。上記の方法で列名を補間するにはどうすればよいですか?