Say I have a controller that returns a list of users. The users are to be returned from memcache if the cache key exists, otherwise hit the mysql db. This logic will be re-used in say a web service layer or something.
action:
def list
if in cache
@userlist = ...
else
@userlist = User.all()
end
end
In the Java world, you would create a UserService layer that would wrap additional logic (like first checking the cache layer, etc.).
In rails it people tend to put all this logic in the controller.
What is the Rails 'best-practise' here?
There seems to be a "small" movement in the Rails community to establishing a service layer in some projects/applications. In 2010, I worked on a project where we introduced a apps/services directory to store service objects. We found that the application logic was spread across controllers and models and this helped encapsulate such behaviour. James Golick has an interesting post on the subject. Check out Pat Maddox's comments as well:
http://jamesgolick.com/2010/3/14/crazy-heretical-and-awesome-the-way-i-write-rails-apps.html
The "Rails way" is: skinny controllers, fat models.
You can simply change the model to support cache:
class User < ActiveRecord::Base
def self.all
@cached[:all] ||= super
end
end
Or create an injector to support cache the way you want for multiple models:
class User < ActiveRecord::Base
include CacheInjector
end
Remember: Ruby, as a dynamic language, is very easy to extend. Mixins, interceptors, aspects, all those things that are a PITA to implement in Java, are very easy and natural on Ruby. Give it a try.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With