views:

50

answers:

2

Hi, I have the following code in my project`s lib directory

module Pasta  
  module ClassMethods
    def self.has_coordinates
      self.send :include, InstanceMethods     
    end
  end

  module InstanceMethods
    def coordinates
      [longitude ||= 43.0, latitude ||= 25.0]
    end
  end   

  ActiveRecord::Base.extend ClassMethods
end

And it should create a class method for ActiveRecord::Base - has_coordinates - which I can "assign" to models... But I receive the error undefined local variable or method 'has_coordinates'

Thanks in advance!

+1  A: 

Dropping the self. in ClassMethods should do the trick.

module Pasta  
  module ClassMethods
    def has_coordinates
      self.send :include, InstanceMethods
    end
  end

  module InstanceMethods
    def coordinates
      [longitude ||= 43.0, latitude ||= 25.0]
    end
  end   

  ActiveRecord::Base.extend ClassMethods
end
elektronaut
But it does not... :(Even if I make a syntax error in pasta.rb, I do not receive it... Is Rails including the file at all?
Dimitar Vouldjeff
Not unless you tell it to. You'll probably have to <code>require 'pasta'</code> somewhere.
elektronaut
A: 

Try this:

module Pasta
  def has_coordinates
    send :include, InstanceMethods
  end

  module InstanceMethods
    def coordinates 
      [longitude ||= 43.0, latitude ||= 25.0] 
    end     
  end
end

ActiveRecord::Base.extend Pasta
John Topley
Same thing... PS. I am using Rails 2.3.5
Dimitar Vouldjeff
0.o I fixed things up... Thanks!
Dimitar Vouldjeff