1

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.

BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • What do you mean by internal service layer? Is it specific application logic outside of models/controllers or something else? – Draiken Sep 02 '11 at 18:50
  • Example: you want to provide a service to talk to a remote ERP or HR system via its REST API. But you want to centralize the logic for this work, not let it spread into controllers. And your not sure yet that the code your working on rises to the level of a plugin/gem yet. – BuddyJoe Sep 03 '11 at 00:24
  • Was gonna answer what Jeff answered below, wasn't sure if it was what you wanted, but I think it is. Normally logic specific to your app that is not a controller is stored into `lib` folder. Remember to encapsulate things in a module so you don't have to worry about class collisions. – Draiken Sep 05 '11 at 13:09

1 Answers1

3

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
Jeff Perrin
  • 8,094
  • 1
  • 23
  • 19
  • Should these autoload_path'd classes also be available during Rails Console mode? – BuddyJoe Sep 05 '11 at 15:53
  • Yes, as far as I can tell. My understanding, which seems correct after doing a simple test, is that anything under /app will be loaded automatically. So if you put services under /app/services, they'll just be there. Adding another autoload directory in the root, (/services for example) should behave the same way that /lib does. Files in /lib *are* loaded in the rails console. The rails console should load the exact same way as your app does when running 'rails server' – Jeff Perrin Sep 06 '11 at 13:03
  • Should the line really be - config.autoload_paths += %W(#{config.root}/lib/services ? or is the autoload paths supposed to be recursive into subdirectories? I could not get it to work without this. – BuddyJoe Sep 06 '11 at 13:53
  • Actually even worse - I remember now I had to do a require 'C:/projects/testproject/lib/services/test_service' when doing rails console. Is there a way to tell where config.root points to from the console? – BuddyJoe Sep 06 '11 at 14:32
  • This was helpful in getting things to work correctly in my app - http://stackoverflow.com/questions/4074830/adding-lib-to-config-autoload-paths-in-rails-3-does-not-autoload-my-module – BuddyJoe Sep 06 '11 at 14:40
  • See this answer: http://stackoverflow.com/questions/4074830/adding-lib-to-config-autoload-paths-in-rails-3-does-not-autoload-my-module/4074861#4074861 If you put your files under lib/services try namespacing your classes/modules to follow the folder structure: 'class Services::MyService' instead of just 'class Service'. – Jeff Perrin Sep 06 '11 at 14:41