插件在开发模式下未重新加载

发布于 2024-11-29 21:36:26 字数 1611 浏览 2 评论 0原文

我编写的插件遇到了一个奇怪的问题。奇怪的是,我还有另一个插件可以打开各种 ActiveRecord 类,并且自动重新加载没有问题。

在插件 init.rb 文件中,

User.class_eval do
  has_one :reputation
  include Karma 
  alias :rep :reputation
end

如果我在生产模式或 Rails 控制台中运行它,则没有问题。当我在开发中运行它时,它会加载一次,但除非我重新启动服务器,否则不会再次加载。

我在我使用的其他插件(acts_as_read)中找不到任何特别的东西,它以相同的方式打开用户。


编辑:

我做了:

ActiveRecord::Base.class_eval do
  class << self
    def has_karma_values
      has_one :reputation
      alias :rep :reputation
    end
  end
end

在插件的 init.rb 文件中并做了

class User < ActiveRecord::Base
...
has_karma_values
...
end

并得到了有关 has_karma_values 不存在的相同错误。此时该插件尚未加载。

令人困惑的是,行为可读完全没有问题,

User.class_eval do
  has_many :readings
end

ActiveRecord::Base.send :include, ActiveRecord::Acts::Readable

但我的却有问题。

这是我完整的 init.rb 文件

require 'reputation'
require 'karma_name'
require 'karma_tag'
require 'karma_title'
require 'acts_as_karmable'
require 'karma'
require File.expand_path('../lib/generators/karma_generator', __FILE__)

KarmaTitle.setup 'Default'

ActiveRecord::Base.class_eval do
  class << self
    def has_karma_values
      has_one :reputation
      alias :rep :reputation
    end
  end
end

ActiveRecord::Base.send :include, ActiveRecord::Acts::Karmable

这是错误 /home/david/apps/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/base.rb: 1014:inmethod_missing':未定义局部变量或方法has_karma_values' #<类:0x9ad1b24> (名称错误)

I am having a weird problem with a plugin I wrote. What is weird is that I have one other plugin that opens various ActiveRecord classes and it has no problems auto-reloading.

In the plugins init.rb file I have

User.class_eval do
  has_one :reputation
  include Karma 
  alias :rep :reputation
end

If I run it in production mode or in rails console there are no issues. When I run it in development it does load it once, but never again unless I restart the server.

I couldn't find anything special in the other plugin I am using(acts_as_readable) and it opens User in the same manner.


Edit:

I did:

ActiveRecord::Base.class_eval do
  class << self
    def has_karma_values
      has_one :reputation
      alias :rep :reputation
    end
  end
end

in the plugiin's init.rb file and did

class User < ActiveRecord::Base
...
has_karma_values
...
end

And get the same error about has_karma_values not existing. The plugin hasn't been loaded at this point.

What is confusing is that acts-as-readable has no issues at all with

User.class_eval do
  has_many :readings
end

ActiveRecord::Base.send :include, ActiveRecord::Acts::Readable

but yet mine does.

Here is my complete init.rb file

require 'reputation'
require 'karma_name'
require 'karma_tag'
require 'karma_title'
require 'acts_as_karmable'
require 'karma'
require File.expand_path('../lib/generators/karma_generator', __FILE__)

KarmaTitle.setup 'Default'

ActiveRecord::Base.class_eval do
  class << self
    def has_karma_values
      has_one :reputation
      alias :rep :reputation
    end
  end
end

ActiveRecord::Base.send :include, ActiveRecord::Acts::Karmable

Here is the error /home/david/apps/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.9/lib/active_record/base.rb:1014:inmethod_missing': undefined local variable or method has_karma_values' for #<Class:0x9ad1b24> (NameError)

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

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

发布评论

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

评论(2

窗影残 2024-12-06 21:36:26

这应该有效:

# plugin init.rb
Rails.configuration.to_prepare do
  User.class_eval do
    has_one :reputation
    include Karma 
    alias :rep :reputation
  end
end

This should work:

# plugin init.rb
Rails.configuration.to_prepare do
  User.class_eval do
    has_one :reputation
    include Karma 
    alias :rep :reputation
  end
end
多彩岁月 2024-12-06 21:36:26

初始化文件加载一次,您不应该从插件访问模型,应该是相反的,您的初始化文件可能应该是这样的:

ActiveRecord::Base.class_eval do
  class << self 
    def acts_as_readable
      has_one :reputation
      include Karma
      alias :rep :reputation
    end
  end
end

做:

class User < ActiveRecord::Base
  acts_as_readable
end

在您的 user.rb 文件中,您应该这样 应该为您提供您期望的功能。

The init file is loaded once, you should not access your models from your plugin, it should be the other way around, your init file should probably be like this:

ActiveRecord::Base.class_eval do
  class << self 
    def acts_as_readable
      has_one :reputation
      include Karma
      alias :rep :reputation
    end
  end
end

And at your user.rb file you should do it like this:

class User < ActiveRecord::Base
  acts_as_readable
end

And this should give you the functionality you expect.

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