I am trying to generate some count methods dynamically for a given array of model names that I can then use in a view/helper:
# create dynamic count methods for each model we want
['model', 'other_model', 'next_model'].each do |name|
class_eval{
"def total_#{name.underscore}s_count
total_#{name.underscore}s_count ||= #{name.camelcase}.all.count
end"
}
end
However, I have a few questions:
- Where should this code go if I want to be able to call these methods in a view?
- What class would these methods be added to? For instance, how would I go about calling them since I'm not sure if they belong to the User, etc. class since they are for a bunch of models.
- Is there a better way of doing this?