2011-12-15 158 views
1

我有一個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的版本是從GitHub 3.2.0.pre,Ruby是1.9.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)每一個請求之前運行一次。

+0

Thanks.In的config.to_prepare塊,自我是ActionDispatch :: Reloader.So self.customize_file和self.customize_user的一個實例不能在confing.to_prepare塊被調用。 – 2011-12-15 10:55:10

相關問題