2012-11-18 17 views
1

我在文件LIB的方法/我的/ column.rb如何包括我自己在軌方法

module Column 
    def array_each_col(num_of_cols, array_arg) 
    .. code will return array .. 
    end 
end 

我希望在我的模型

class Area < ActiveRecord::Base 
    include Column 


    def self.my_areas 
    all_areas = Area.all  
    num_of_cols = 2  
    self.array_each_col(num_of_cols, all_areas) 
    end 
end 

我的應用程序中使用它。 RB

module MyAppName 
    class Application < Rails::Application 
    config.autoload_paths << "#{config.root}/lib/my" 

但它不工作

+0

我會用我的方法不同模型 – user1466717

+1

「不起作用」不是任何有效的描述。你期望發生什麼,你觀察到了什麼?你有什麼努力去匹配兩者?基本上http://whathaveyoutried.com –

+0

未定義的方法'array_each_col'爲# user1466717

回答

4

馬科類方法可用模塊的方法,你必須使用extend而非include

+0

Works。但是在這種情況下,array_each_col和self.array_each_col有什麼不同?在兩種情況下都可以工作 – user1466717

+0

這是一樣的!你可以省略'self' –

0

嘗試config.autoload_paths < < 「#{Rails的 .root}/lib中/我的」