生产模式下的 Rails 依赖问题

发布于 2024-07-26 02:41:17 字数 2130 浏览 3 评论 0原文

我正在开发一个遗留的 Oracle 数据库,该数据库具有稍微奇怪的表命名约定,其中每个列名都以表首字母为前缀 - 例如policy.poli_id。

为了使该数据库更易于使用,我有一个方法 set_column_prefix ,它为删除了前缀的每列创建访问器。 即:

# Taken from wiki.rubyonrails.org/rails/pages/howtouselegacyschemas
class << ActiveRecord::Base
  def set_column_prefix(prefix)
    column_names.each do |name|
      next if name == primary_key

      if name[/#{prefix}(.*)/e]
        a = $1

        define_method(a.to_sym) do
          read_attribute(name)
        end

        define_method("#{a}=".to_sym) do |value|
          write_attribute(name, value)
        end

        define_method("#{a}?".to_sym) do
          self.send("#{name}?".to_sym)
        end

      end
    end
  end
end

它位于我的 lib/ 目录中的文件 (insoft.rb) 中,并且在 Rails::Initializer.run 块之后从我的 config/environment.rb 中需要。

这在开发中运行良好,但是当我尝试在生产模式下运行应用程序时,我在所有模型中都会收到以下错误:

dgs@dgs-laptop:~/code/voyager$ RAILS_ENV=production script/server 
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1964:in `method_missing': 
undefined method `set_column_prefix' for #<Class:0xb3fb81d8> (NoMethodError)
    from /home/dgs/code/voyager/app/models/agent.rb:16

此错误是由 config/environments/ 中的 'config.cache_classes = true' 行触发的生产.rb。 如果我将其设置为false,那么rails将启动,但不会缓存类。 我猜这使得rails在运行Initializer块之前缓存所有模型

如果我将'require“insoft.rb'”移动到Rails::Initializer.run块开始之前,那么我会收到错误,因为ActiveRecord没有尚未初始化:

usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in `load_missing_constant': uninitialized constant ActiveRecord (NameError)
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:92:in `const_missing'
    from /home/dgs/code/voyager/lib/insoft.rb:1

我应该在哪里包含此自定义库和 set_column_prefix 方法,以便在模型缓存之前但在加载所有 activerecord 文件之后拾取它?

干杯

戴夫·斯迈利

Im working on a legacy oracle database with a slightly odd table naming convention where each column name is prefixed with the tables initial letters - eg policy.poli_id.

To make this database easier to work with I have a method set_column_prefix that creates accessors for each column with the prefix removed. ie:

# Taken from wiki.rubyonrails.org/rails/pages/howtouselegacyschemas
class << ActiveRecord::Base
  def set_column_prefix(prefix)
    column_names.each do |name|
      next if name == primary_key

      if name[/#{prefix}(.*)/e]
        a = $1

        define_method(a.to_sym) do
          read_attribute(name)
        end

        define_method("#{a}=".to_sym) do |value|
          write_attribute(name, value)
        end

        define_method("#{a}?".to_sym) do
          self.send("#{name}?".to_sym)
        end

      end
    end
  end
end

This is in a file (insoft.rb) in my lib/ directory, and required from from my config/environment.rb after the Rails::Initializer.run block.

This has been working fine in development, but when I try to run the application in production mode, I get the following error in all of my models:

dgs@dgs-laptop:~/code/voyager$ RAILS_ENV=production script/server 
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1964:in `method_missing': 
undefined method `set_column_prefix' for #<Class:0xb3fb81d8> (NoMethodError)
    from /home/dgs/code/voyager/app/models/agent.rb:16

This error is triggered by the 'config.cache_classes = true' line in config/environments/production.rb.
If I set this to false, then rails will start up, but won't be caching classes. I'm guessing this makes rails cache all the models before it runs the Initializer block

If I move the 'require "insoft.rb'" to before the start of the Rails::Initializer.run block, then I get errors because ActiveRecord hasn't been initialized yet:

usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:443:in `load_missing_constant': uninitialized constant ActiveRecord (NameError)
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:92:in `const_missing'
    from /home/dgs/code/voyager/lib/insoft.rb:1

Where should I be including this custom lib and set_column_prefix method in order for it to be picked up before the models are cached, but after all the activerecord files have loaded?

Cheers

Dave Smylie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-08-02 02:41:18

我应该在哪里包含这个自定义库和 set_column_prefix 方法,以便在模型缓存之前、所有 activerecord 文件加载之后被拾取?

尝试设置初始化程序。 您可以使用猴子补丁的内容将其命名为 config/initializers/insoft.rb :

class << ActiveRecord::Base
  def set_column_prefix(prefix)
    column_names.each do |name|
      next if name == primary_key

      if name[/#{prefix}(.*)/e]
        a = $1

        define_method(a.to_sym) do
          read_attribute(name)
        end

        define_method("#{a}=".to_sym) do |value|
          write_attribute(name, value)
        end

        define_method("#{a}?".to_sym) do
          self.send("#{name}?".to_sym)
        end

      end
    end
  end
end

Where should I be including this custom lib and set_column_prefix method in order for it to be picked up before the models are cached, but after all the activerecord files have loaded?

Try setting up an initializer. You can call it config/initializers/insoft.rb with the contents of your monkey patch:

class << ActiveRecord::Base
  def set_column_prefix(prefix)
    column_names.each do |name|
      next if name == primary_key

      if name[/#{prefix}(.*)/e]
        a = $1

        define_method(a.to_sym) do
          read_attribute(name)
        end

        define_method("#{a}=".to_sym) do |value|
          write_attribute(name, value)
        end

        define_method("#{a}?".to_sym) do
          self.send("#{name}?".to_sym)
        end

      end
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文