How do you scope ActiveRecord associations in Rails 3?

I suggest you take a look at “Named scopes are dead”

The author explains there how powerful Arel is 🙂

I hope it’ll help.

EDIT #1 March 2014

As some comments state, the difference is now a matter of personal taste.

However, I still personally recommend to avoid exposing Arel’s scope to an upper layer (being a controller or anything else that access the models directly), and doing so would require:

  1. Create a scope, and expose it thru a method in your model. That method would be the one you expose to the controller;
  2. If you never expose your models to your controllers (so you have some kind of service layer on top of them), then you’re fine. The anti-corruption layer is your service and it can access your model’s scope without worrying too much about how scopes are implemented.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)