我有一个users_manager引擎,它有一个User
模型类。
在其他购物引擎中,我在User
模型中添加了以下代码中的一些关联,位于shopping/lib/shopping.rb
:
module Shopping
class Engine<Rails::Engine
initializer :shopping_append_user do
UsersManager::User.class_eval do
has_many :products,:class_name=>"Shopping::Product"
has_many :virtues,:class_name=>"Shopping::Virtue"
has_many :containers,:class_name=>"Shopping::Container"
has_many :concerns,:class_name=>"Shopping::Concern"
has_many :remarks,:class_name=>"Shopping::Remark"
has_many :praisings,:class_name=>"Shopping::Praising"
has_one :cart,:class_name=>"Shopping::Cart"
has_one :shop_information,:class_name=>"Shopping::ShopInformation"
has_many :comments,:class_name=>"Shopping::Comment"
has_many :created_orders,:class_name=>"Shopping::Order",:foreign_key=>"creator_id"
has_many :processing_orders,:class_name=>"Shopping::Order",:foreign_key=>"processor_id"
end
end
initializer :shopping_append_file do
Upload::File.class_eval do
has_many :image_uuids,:class_name=>"Shopping::ImageUuid"
end
end
end
def self.table_name_prefix
"shopping_"
end
end
运行rails server
后,应用程序正常运行。但是,在修改一个控制器文件后,我浏览了网页,它给了我以下消息:
undefined method `products' for #<UsersManager::User:0x00000003022a58>
rails如何在修改文件后重新加载文件?如何让我的引擎正常工作?
我的rails版本是github的3.2.0.pre,Ruby是1.9.0。
答案 0 :(得分:1)
每次请求都不会重新加载初始值设定项,这意味着重新加载UsersManager::User
类的自定义设置时会丢失。
您可以执行以下操作:
module Shopping
class Engine < Rails::Engine
config.to_prepare do
Shopping.customize_user
Shopping.customize_file
end
end
def self.customize_user
UsersManager::User.class_eval do
has_many :products,:class_name=>"Shopping::Product"
has_many :virtues,:class_name=>"Shopping::Virtue"
has_many :containers,:class_name=>"Shopping::Container"
has_many :concerns,:class_name=>"Shopping::Concern"
has_many :remarks,:class_name=>"Shopping::Remark"
has_many :praisings,:class_name=>"Shopping::Praising"
has_one :cart,:class_name=>"Shopping::Cart"
has_one :shop_information,:class_name=>"Shopping::ShopInformation"
has_many :comments,:class_name=>"Shopping::Comment"
has_many :created_orders,:class_name=>"Shopping::Order",:foreign_key=>"creator_id"
has_many :processing_orders,:class_name=>"Shopping::Order",:foreign_key=>"processor_id"
end
end
def self.customize_file
Upload::File.class_eval do
has_many :image_uuids,:class_name=>"Shopping::ImageUuid"
end
end
def self.table_name_prefix
"shopping_"
end
end
config.to_prepare
块在生产中和开发中的每个请求(source)之前运行一次。