On Sunday 13 September 2009 11:18:52 am Gaspard Bucher wrote:
> Damned ! This means that all the dynamic methods created by rails with
> "has_one" and such cannot be overwritten by including a module except if
> you write:
>
> module A
>   def self.included(base)
>     base.send(:define_method, 'foo') do
>       puts "'foo' from A"
>     end
>   end
> end
>
> Any other solution ?

As others said, drop the 'define_method'. I'd take it a step further:

module A
  module ClassMethods
    def foo
      puts "'foo' from A"
    end
  end
  def self.included(base)
    base.send :extend, ClassMethods
  end
end

This is a common idiom. It's not so much load order as the fact that class 
methods don't automatically get included by "include" -- but they are also 
instance methods on the class, if that makes sense.

The advantage of doing it this way is that you can put a lot more stuff in 
ClassMethods, or even mix in other modules (via "include") inside 
ClassMethods. It's also nice and self-documenting, and it's used all over the 
place -- I believe inside Rails, at least.

I would even go so far as to call this a best practice. Thoughts?