default-scope

Rails 4 default scope

99封情书 提交于 2019-11-26 22:36:58
问题 In my Rails app have a default scope that looks like this: default_scope order: 'external_updated_at DESC' I have now upgraded to Rails 4 and, of course, I get the following deprecation warning "Calling #scope or #default_scope with a hash is deprecated. Please use a lambda containing a scope.". I have successfully converted my other scopes but I don't know what the syntax for default_scope should be. This doesn't work: default_scope, -> { order: 'external_updated_at' } 回答1: Should be only:

Why is using the rails default_scope often recommend against?

假装没事ソ 提交于 2019-11-26 08:02:09
问题 Everywhere on the internet people mention that using the rails default_scope is a bad idea, and the top hits for default_scope on stackoverflow are about how to overwrite it. This feels messed up, and merits an explicit question (I think). So: why is using the rails default_scope recommended against? 回答1: Problem 1 Lets consider the basic example: class Post < ActiveRecord::Base default_scope { where(published: true) } end The motivation to make the default published: true , might be to make