需要在 rails3 的自定义模块中使用 `memoize`
need to use `memoize` in custom module in rails3
我在 lib
文件夹中有一个 module
。我在其中创建了一个 InstanceMethods
模块。
require 'memoist'
extend Memoist
module MyStudentMethods
def self.included base
base.send :include, InstanceMethods
end
module InstanceMethods
def find_student(user_id)
self.student.find{|student| student.user_id == user_id }
end
memoize :find_student
end
end
我在 model
中使用这个 module
。
require_dependency 'my_student_methods'
class Student < ActiveRecord::Base
# previously I was using memoize like below commented out code
# require 'memoist'
# extend Memoist
include MyStudentMethods
end
但是当我启动 rails server
它时出现错误:
/home/Projects/school/lib/my_student_methods.rb:12:in `<module:InstanceMethods>': undefined method `memoize' for MyStudentMethods::InstanceMethods:Module (NoMethodError)
问题:如何memoize
模块方法?
尝试将 extend Memoist
移动到您的模块中。像这样:
module InstanceMethods
extend Memoist
...
module InstanceMethods
extend Memoist
...
我在 lib
文件夹中有一个 module
。我在其中创建了一个 InstanceMethods
模块。
require 'memoist'
extend Memoist
module MyStudentMethods
def self.included base
base.send :include, InstanceMethods
end
module InstanceMethods
def find_student(user_id)
self.student.find{|student| student.user_id == user_id }
end
memoize :find_student
end
end
我在 model
中使用这个 module
。
require_dependency 'my_student_methods'
class Student < ActiveRecord::Base
# previously I was using memoize like below commented out code
# require 'memoist'
# extend Memoist
include MyStudentMethods
end
但是当我启动 rails server
它时出现错误:
/home/Projects/school/lib/my_student_methods.rb:12:in `<module:InstanceMethods>': undefined method `memoize' for MyStudentMethods::InstanceMethods:Module (NoMethodError)
问题:如何memoize
模块方法?
尝试将 extend Memoist
移动到您的模块中。像这样:
module InstanceMethods
extend Memoist
...
module InstanceMethods
extend Memoist
...