将 Ruby 扩展方法添加到 Rails 3 的最佳实践? [英] Best practice for adding a Ruby extension methods to Rails 3?
问题描述
我想在我的 Rails 3 项目中使用一个数组扩展方法.它应该住在哪里?
I have an Array extension method I want to use in my Rails 3 project. Where should it live?
我有一个最初放置它的应用程序/类 (array_extensions.rb),并在我的 config/application.rb 中加载路径:config.autoload_paths += %W(#{Rails.root}/app/classes)
.但是,当我转到 rails 控制台
时,未加载扩展.
I have a app/classes where I originally put it (array_extensions.rb), and in my config/application.rb I load the path: config.autoload_paths += %W(#{Rails.root}/app/classes)
. However, when I drop to rails console
the extension is not loaded.
是否有一个预先定义的地方可以放置我的 Rails 3 扩展方法?或者,一种预先定义的方式来添加它们?我知道 Rails 有它自己的 Array 扩展方法.我应该将我的添加到 active_support/core_ext/array/conversions.rb
吗?
Is there a pre-definded place I should put my extension methods for Rails 3? Or, a pre-definded way to add them? I know Rails has it's own extensions methods for Array. Should I add mine to active_support/core_ext/array/conversions.rb
?
Rails 3 的最佳实践是什么?
推荐答案
更好的方法是在 lib/core_ext
目录中创建您的扩展,以便轻松了解您的 core_ext 在哪里.
The better way is create your extension in lib/core_ext
directory to understand easyly where is your core_ext.
在创建初始化程序以需要此文件之后.
After create an initializer to require this file.
这篇关于将 Ruby 扩展方法添加到 Rails 3 的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!