开发者

Where's the best practice location for non-table-related classes in Rails apps?

Where do most Rails apps usually keep their non-active-record classes?

In app/models? In lib/some_file.rb?

Som开发者_StackOverflow中文版eplace else?


It depends what is the purpose of this class. If this is something like a model. On example, by default mailers are put in app/models and it isn't ActiveRecord. If you want to add some classes that are used in your application in many places and it isn't model-related, then probably the best place is to put it in a lib directory. If you want to reuse it, you can also create a plugin with your class and then it would be placed in vendor/plugins.

You can also put it in config/initializers if it fits into this category.

To sum up: it depends what your class is about. Put it in a place where it fits. If you have doubts, put it in lib directory.


If you put your files in app/models, they'll be reloaded along with all other models. This eases development. In app/models, I have:

acts_as_bookable.rb
subclass_responsibility_error.rb
user.rb

To Rails, app/models is just a location from which it reloads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜