Question

When you want to write an internal service layer in Rails 3, where should you place the code? I'm coming from a Grails background and wondering what the service equivalent is in Rails.

Also, if this is covered in articles or online videos - would love to have the links. Thanks.

Was it helpful?

Solution

The Rails convention in this case is usually to put this stuff in the lib folder (ie; lib/services/erp_service.rb). I've created a 'Services' folder under the 'app' directory to contain services like this in the past as well. I don't believe you need to do anything extra to make the classes load, however if you want to put your services under the 'app' directory and they don't appear to be loading you can try messing with config.autoload_paths option from application.rb (in a Rails 3.x application)

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top