Not able to call the `super` while creating methods with #define_method method

Here is my code :

class Menu < ActiveRecord::Base
  
  Dish::Types.each do |type|
    has_one type, -> { where dish_type: type }, class_name: 'Dish'
  end

  Dish::Types.each do |type|
    define_method(type) { super() || NullDish.new } # tried super || NullDish.new
  end
  
  # ...
end

When I am calling this method :

Menu.first.send :breakfast
# RuntimeError: implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly.

How to resolve this ?

···

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

I attempted because I got some hints from here : http://stackoverflow.com/a/20132555/2767755

With my approach it didn't work. The author of the answer said , not to use alias_method, but that only works for my case :

class Menu < ActiveRecord::Base
  
  Dish::Types.each do |type|
    has_one type, -> { where dish_type: type }, class_name: 'Dish'
  end

  Dish::Types.each do |type|
    alias_method("old_#{type}", type)

    define_method(type) do
      send("old_#{type}") || NullObject::NullDish.new
    end
  end
  #...
end

Still, wanted to know, why `super` is not worked ?

···

On Thursday, February 05, 2015 12:40:37 AM Arup Rakshit wrote:

Here is my code :

class Menu < ActiveRecord::Base
  
  Dish::Types.each do |type|
    has_one type, -> { where dish_type: type }, class_name: 'Dish'
  end

  Dish::Types.each do |type|
    define_method(type) { super() || NullDish.new } # tried super || NullDish.new
  end
  
  # ...
end

When I am calling this method :

Menu.first.send :breakfast
# RuntimeError: implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly.

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan