0

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!

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Not unless you tell it to. You'll probably have to <code>require 'pasta'</code> somewhere.
0

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

2 Comments

Same thing... PS. I am using Rails 2.3.5
0.o I fixed things up... Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.