使用 Gem 中的类和实例方法扩展 Mongoid
在ActiveRecord
中,我习惯这样做:
ActiveRecord::Base.send :include, MyGem::ActsAsMagnific::Base
而且它的工作方式就像一个魅力。
但我无法在 Mongoid
中获得相同的结果。我尝试了这个但没有成功:
Mongoid::Document::ClassMethods.class_eval do
include MyGem::ActsAsMagnific::Base
end
我正在使用rails3并且我必须包含类和实例方法。
in ActiveRecord
, I'm used to do this:
ActiveRecord::Base.send :include, MyGem::ActsAsMagnific::Base
And it works like a charm.
But I can't achieve the same result in Mongoid
. I tried this without success:
Mongoid::Document::ClassMethods.class_eval do
include MyGem::ActsAsMagnific::Base
end
I'm using rails3 and I've to include class and instance methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想向 Mongoid::Document 添加类方法,也许您应该尝试扩展它。
无论如何,最好使用 ActiveSupport::Concern 来完成这样的任务。
例如看这篇文章:
使用 ActiveSupport::Concert< /a>
If you want to add class methods to Mongoid::Document, maybe you should try to extend it.
In any case, it's preferable to use ActiveSupport::Concern for task like this.
For example see this post:
Using ActiveSupport::Concert
我无法对上面的答案发表评论,但替换 Roman 所说的新 URL 是:
I can't comment on the answer above, but the new URL that replaces the one that Roman stated is: http://pivotallabs.com/using-activesupport-concern-for-easy-mix-ins/