0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsのService層のクラスをauto_loadpathに含める

Posted at

環境

  • Ruby 2.4.2
  • Rails 5.1.4

やりたいこと

app
- services/
- neko.rb
- - utils/
- - - hoge.rb
- - users/
- - - test.rb

みたいなserivcesディレクトリ構成があるとして、全てのディレクトリ内のファイルをオートロードパスに含めたいが、デフォルトでは出来ないので、その方法について記載します。

デフォルトでは、servicesディレクトリの直下のみがオートロードパスの対象となっています。

services.rbを編集

config/initializersディレクトリ内にserices.rbを作成します(自分の環境にはありませんでした)。

services.rbを以下のようにします。

Dir.glob("app/services/**/*.rb") do |file|
  require_dependency Rails.root.join(file)
end

これで全てのファイルが対象となっているかと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?