Federico Brubacher wrote:
> The names are all right but within the block I get for all the methods I
> created my last element (element N) in my $ingredients_column variable .
> 
> for column_name in $ingredients_columns do
> 
>    define_method("sort_by_" + column_name.human_name) {  @ingredients =
> Ingredient.find(:all, :order => "#{column_name.human_name  } DESC")}
> 
> end

I'm not really experienced, but I guess your problem is caused because
the block you pass to define_method isn't evaluated at the time you pass
it to define_method, but when you actually call the created methods.
This means the local variables are evaluated at that time too.

You may want to use eval instead if you want variables expanded at
define-time, and their value be included in the body of the newly
defined methods (the string you pass to eval gets interpolated first
(variable references expanded), and then the resulting ruby code
executed - thus the values effective just when eval is called get
incorporated in the defined methods' bodies):

for column_name in $ingredients_columns do
  eval %Q[
    def sort_by_#{column_name.human_name} {
      @ingredients = Ingredient.find(
        :all, :order => "#{column_name.human_name} DESC")
    }
  ]
end

If anyone knows of a better approach, please post it as I'd be
interested too.

mortee