0

I've inherited a rails project that I am looking to refactor. The app uses ActiveRecord very lightly; most of the calls are to REST endpoints. Right now the classes that make these calls are in the lib directory. I've been reading a couple of posts regarding the lib directory, and I'm wondering the rationale why the original developer did not use the models directory (there must be some reason) and what are the pros/cons of moving these classes into the models directory.

It "feels" like these classes should be in the models directory as they are core to the application.

4

2 回答 2

1

这就是我正在做的,将所有负责使用数据库的类放入模型文件夹中。如果是库或类,它不依赖于数据库并且可以在不同的项目中使用,那么它可以放在 lib 文件夹中。

于 2012-08-30T11:01:56.277 回答
1

如果你的类不是完全与模型相关(ActiveRecord),你应该考虑把它放在一个比模型更具体的地方,但是如果你把它放在模型中,把它们放在一个封闭的文件夹中。

app -> models -> endpoints 里面 config -> application.rb,把这行代码放进去加载

config.autoload_paths += %W(#{Rails.root}/app/models/ar)

或者你可以把它放在一个单独的目录 app -> 端点中

这样做的原因是因为您的 REST 端点将被隔离;如果您计划在应用程序上构建更多内容,您的业务逻辑将更加“干净”和模块化,并且不会杂乱无章。

于 2012-08-31T16:38:34.563 回答