I have a method that just iterates through various models, printing out the model attributes:
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
to call the 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 the column name as a string and variable. When passed as a variable I get an undefined variable
exception. As is above, I get an undefined method 'col_name'
exception. How can I interpolate the column name within the above method?
You would represent the column/method name as a symbol (e.g. :revenue
) and invoke it with __send__
as in:
p.__send__(options[:col_name])