Rails - Iterating through records witin a method -
i have method iterates through various models, printing out model attributes:
def self.get_attributes(options = {}) options[:model_name].find(:all, :conditions => {:generated_at => options[:start_date]..options[:end_date]}).each |p| puts p.options[:col_name] end
to call method:
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})
i have tried passing in column name string , variable. when passed variable undefined variable
exception. above, undefined method 'col_name'
exception. how can interpolate column name within above method?
you represent column/method name symbol (e.g. :revenue
) , invoke __send__
in:
p.__send__(options[:col_name])
Comments
Post a Comment