{"issue":{"state":"closed","created_at":"2011-10-19T21:33:00Z","updated_at":"2011-12-06T16:03:19Z","user":{"gravatar_id":"4eceec28506545e8ed6dd49dde519d2e","id":64874,"url":"https://api.github.com/users/lucasas","login":"lucasas","avatar_url":"https://secure.gravatar.com/avatar/4eceec28506545e8ed6dd49dde519d2e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"comments":9,"body":"Hi guys, firstly I would like to thank you for the great work.\r\n\r\nI have the following code:\r\n\r\n```ruby\r\nmodule Test\r\n  extend ActiveSupport::Concern\r\n  included do\r\n    include Mongoid::Document\r\n    field :title, :type => String\r\n    default_scope desc :title\r\n  end\r\nend\r\n\r\nclass Person\r\n  include Test\r\n  field :name, :type => String\r\n  scope :by_name, order_by([[:name, :desc]])\r\nend\r\n```\r\nAnd I run:\r\n\r\n```ruby\r\np1 = Person.create :title => \"abc\", :name => \"abc\"\r\np2 = Person.create :title => \"cba\", :name => \"cba\"\r\n\r\nPerson.all.to_a # [p2, p1] in order\r\nPerson.unscoped.to_a # [p1, p2]\r\n\r\nputs Person.unscoped.by_name.inspect\r\n# <Mongoid::Criteria\r\n#  selector: {},\r\n#  options:  {:sort=>[[:title, :desc], [:name, :desc]]},\r\n#  class:    Person,\r\n#  embedded: false>\r\n```\r\n\r\nAs you can see, when I'm using unscoped included in a Module before a scope defined in a class, Mongoid does not exclude default_scope from criteria object. I had confirmed this when I ran this example:\r\n\r\n```ruby\r\nclass Game\r\n  include Mongoid::Document\r\n  field :name, :type => String\r\n  field :title, :type => String\r\n\r\n  default_scope desc :title\r\n  scope :by_name, order_by([[:name, :desc]])\r\nend\r\n\r\ng1 = Game.create :title => \"abc\", :name => \"abc\"\r\ng2 = Game.create :title => \"cba\", :name => \"cba\"\r\n\r\nGame.all.to_a # [g2, g1] in order\r\nGame.unscoped.to_a # [g1, g2]\r\n\r\nputs Game.unscoped.by_name.inspect\r\n# <Mongoid::Criteria\r\n#  selector: {},\r\n#  options:  {:sort=>[[:title, :desc], [:name, :desc]]},\r\n#  class:    Game,\r\n#  embedded: false>\r\n```\r\n\r\nWhen I define the default_scope in a class, Mongoid works.\r\n\r\nCan you help me?","assignee":null,"id":1998796,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"labels":[{"name":"bug","color":"e10c02","url":"https://api.github.com/repos/mongoid/mongoid/labels/bug"},{"name":"confirmed","color":"e10c02","url":"https://api.github.com/repos/mongoid/mongoid/labels/confirmed"}],"number":1359,"title":"Default scoped in a Module when I'm using unscoped","closed_at":"2011-12-06T15:59:26Z","html_url":"https://github.com/mongoid/mongoid/issues/1359","url":"https://api.github.com/repos/mongoid/mongoid/issues/1359","milestone":{"state":"open","created_at":"2011-10-06T16:38:59Z","closed_issues":12,"due_on":"2011-12-12T08:00:00Z","creator":{"gravatar_id":"6f09262a13427532461b0b618d445b1e","id":9030,"url":"https://api.github.com/users/durran","login":"durran","avatar_url":"https://secure.gravatar.com/avatar/6f09262a13427532461b0b618d445b1e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"number":27,"title":"2.4.0","description":"","url":"https://api.github.com/repos/mongoid/mongoid/milestones/27","open_issues":24}},"action":"created","comment":{"created_at":"2011-12-06T16:03:19Z","updated_at":"2011-12-06T16:03:19Z","user":{"gravatar_id":"6f09262a13427532461b0b618d445b1e","id":9030,"url":"https://api.github.com/users/durran","login":"durran","avatar_url":"https://secure.gravatar.com/avatar/6f09262a13427532461b0b618d445b1e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"So this turned into a complete pain that could not be completely solved without a rewrite of the criteria/scoping code. (Which has already started in the origin repo.) In order not to break the public API in a minor release, I've provided a fix that gets around this issue for now, with the true rewrite coming in 3.0.0.\r\n\r\nSimply in this case, if you now define all the named scopes as lambdas, all will be kosher.\r\n\r\n```ruby\r\nscope :by_name, lambda { order_by([[:name, :desc]]) }\r\n```\r\n\r\nThe problem was that the default scope was getting applied at class load time to all named scopes, and even if you called unscoped applying the named scoped after would still contain the default. A smart removal of the default scope when defining a named scope has too many edge cases to be implemented, so I need to rewrite the way the defaults are applied... This will come soon.","id":3034094,"url":"https://api.github.com/repos/mongoid/mongoid/issues/comments/3034094"}}