Overriding default_scope in Rails

后端 未结 4 1235
北恋
北恋 2020-12-13 07:09

In my Post.rb model, I have default_scope :conditions => {:deleted => \'false\'}

But if I try to run Post.find(:all, :conditions => \"del

4条回答
  •  失恋的感觉
    2020-12-13 07:52

    Scopes are meant to be composable, meaning you can combine a bunch of them and it effectively applies all the conditions. In this case ActiveRecord is just too naive to determine that the explicit condition should negate the first one. It just builds the query joining all the clauses with ANDs. For this reason default_scope has the most utility with the :order clauses which is not composable (in ActiveRecord 2.3's implementation anyway). There is more discussion here.

    Also note that in Rails 3 ActiveRecord is using Arel for a lot of query construction which will greatly increase the power of ActiveRecord query generation while simplifying a lot of the internals. It's likely that with Arel will improve your situation. In the meantime I recommend not putting conditions in a default_scope unless there are rows that you really want to be invisible to your Rails app.

提交回复
热议问题