我在包含某些模型的引擎樣式插件中添加了一些代碼。在我的應用程序中,我想擴展其中的一個模型。我設法通過在初始化程序中包含模塊,將實例和類方法添加到相關模型中。使用模塊在「has_many」插件中擴展模型
然而,我似乎無法添加關聯,回調等我得到'方法未找到'的錯誤。
/libs/qwerty/core.rb
module Qwerty
module Core
module Extensions
module User
# Instance Methods Go Here
# Class Methods
module ClassMethods
has_many :hits, :uniq => true # no method found
before_validation_on_create :generate_code # no method found
def something # works!
"something"
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
end
end
end
/initializers/qwerty.rb
require 'qwerty/core/user'
User.send :include, Qwerty::Core::Extensions::User
問題:在做'重新加載!'時在控制檯中,類會重新加載,但由於模塊(在我的情況下)是從初始化程序調用的,因此該模塊不會重新應用。 – Kris 2009-10-07 11:07:24
此外,目前看來,只有在使用控制檯時,模塊中的代碼纔會被包含,當從控制器調用相同的代碼時,它會失敗。我想發佈一個代碼示例,但答案似乎不正確(畢竟這不是一個論壇)... – Kris 2009-10-07 11:09:15
CONTROLLER:render:text => User.new.respond_to?('hits')並返回# => false CONSOLE:User.new.respond_to?('hits')#=> true PREEMPT:我重新啓動了服務器:) – Kris 2009-10-07 11:15:23