Search code examples
ruby-on-railsruby-on-rails-3activerecorddefault

ActiveRecord model defaults for excluding certain query conditions?


Is it possible in AR to exclude certain conditions by default so you don't have to define them in all your application queries?

  • for example exclude for model Profile all where Profile.activate == false?

I have lots of AR queries and now have in each of them a statement .where(:active => true ) to prevent the false one's to be added to the recordset.


Solution

  • Check out default_scope, it seems to do what you want: http://apidock.com/rails/ActiveRecord/Scoping/Default/ClassMethods/default_scope