English 中文(简体)
包括种子中的铁道结初始化。 rb in Railways 3.1
原标题:Include Railtie initialization in seeds.rb in Rails 3.1

我制造了一条简单的铁路,为积极通融增加了一只 st子:

  0 module Searchable
  1   class Railtie < Rails::Railtie
  2     initializer  searchable.model_additions  do
  3       ActiveSupport.on_load :active_record do
  4         extend ModelAdditions
  5       end
  6     end
  7   end
  8 end

在申请被传唤之前,我要求(在校正/校正)在座标/环境。

require  searchable 

这在我的申请中发挥了巨大作用,没有重大问题。

然而,我遇到了一个 r问题:见到。

In my seeds.rb file, I read data in from a csv and populate the database. The problem I am having is that the additions I made to ActiveRecord don t get loaded, and seeds fails with a method_missing error. I am not calling these methods, but I assume that since seeds.rb loads the models, it tries to call some of the methods and that s why it fails.

任何人都能够告诉我,提出这种要求,以便每次都包括进来。 主动记录是装满的(不仅仅是装满申请时)? 我更愿意将守则置于我的模式之外,因为守则是我大多数模式之间共有的,我希望保持其清洁和危险。

问题回答

仅此扩大范围就增加了“积极观察:基本”的内容。

如果参照一个模型级,通过铁路3.1自动载荷/固定式电梯,它将装上该舱。 此时此刻,它只是从本质上讲,对所发生的情况(没有想象力)。 因此,我认为你们至少有几种选择。 那种“坏”的选择是,你想要把这种选择放在依赖性负荷上。 也许像:

module ActiveSupport
  module Dependencies
    alias_method(:load_missing_constant_renamed_my_app_name_here, :load_missing_constant)
    undef_method(:load_missing_constant)
    def load_missing_constant(from_mod, const_name)
       # your include here if const_name =  ModelName 
       # perhaps you could list the app/models directory, put that in an Array, and do some_array.include?(const_name)
       load_missing_constant_renamed_my_app_name_here(from_mod, const_name)
    end
  end
end

这样做的另一个方式是使用像你那样的铁路网,为主动通融增加一个舱级方法:然后包括 st脚的底象:

module MyModule
  class Railtie < Rails::Railtie
    initializer "my_name.active_record" do
      ActiveSupport.on_load(:active_record) do
        # ActiveRecord::Base gets new behavior
        include ::MyModule::Something # where you add behavior. consider using an ActiveSupport::Concern
      end
    end
  end
end

如果使用积极支持:

module MyModule
  module Something
    extend ActiveSupport::Concern

    included do
      # this area is basically for anything other than class and instance methods
      # add class_attribute s, etc.
    end

    module ClassMethods
      # class method definitions go here

      def include_some_goodness_in_the_model
        # include or extend a module
      end 
    end

    # instance method definitions go here
  end
end

然后在每一种模式中:

class MyModel < ActiveRecord::Base

  include_some_goodness_in_the_model

  #...

end

然而,这比在我建议的每一模式中都包含一个内容要好得多。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签