插件在开发模式下未重新加载
我编写的插件遇到了一个奇怪的问题。奇怪的是,我还有另一个插件可以打开各种 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:in
method_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:in
method_missing': undefined local variable or method has_karma_values' for #<Class:0x9ad1b24> (NameError)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
This should work:
初始化文件加载一次,您不应该从插件访问模型,应该是相反的,您的初始化文件可能应该是这样的:
做:
在您的 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:
And at your user.rb file you should do it like this:
And this should give you the functionality you expect.