This project is archived and is in readonly mode.
Merge default scopes by default
-
David Chelimsky
- Assigned user changed from José Valim to Yehuda Katz (wycats)
-
David Chelimsky
- Tag set to activecord, default_scope
- Assigned user changed from Yehuda Katz (wycats) to Pratik
-
David Chelimsky
(sorry for the "Assigned user" spam - just changing that value to get this on the radar of more than one person)
-
José Valim
- Milestone cleared.
- State changed from new to open
- Assigned user changed from Pratik to José Valim
Just discussed with Yehuda and Jeremy and we all agreed! Please do provide a patch! :)
-
David Chelimsky
Excellent - will do so in the next day or so. Looks like it'll be a lot easier to do in rails-3 - nice job finding good abstractions.
-
Repository
- State changed from open to resolved
(from [35a114a8941cb22d29a536f1215a23a8cf7c4756]) Modified default_scope to merge with any pre-existing default_scope and added AR::Base::clear_default_scope
- clear_default_scope provides users who rely on the old behaviour of each call to default_scope overwriting any previous default scopes an opportunity to maintain that behaviour.
[#4583 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/35a114a8941cb22d29a536f1215a23... -
bterkuile
There is still an unsolved issue with this one:
I would like:
class Post
default_scope order(:title) default_scope where(:state => 'active') end
To be similar to:
class Post
default_scope order(:title).where(:state => 'active') end
Since (Rails 3.0.1) activerecord-3.0.1/lib/active_record/base.rb line 890:
Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup
seems to spoil stuff (.dup) I made an initializer with the following content:
class ActiveRecord::Base
def self.default_scope(options = {})key = :"#{self}_scoped_methods" Thread.current[key] = nil self.default_scoping << construct_finder_arel(options, default_scoping.pop)end
end
Basically resetting Thread.current[key] when default_scope is invoked. This solves this issue, but there might be a better solution. -
bterkuile
with better formatting:
class ActiveRecord::Base def self.default_scope(options = {}) key = :"#{self}_scoped_methods" Thread.current[key] = nil self.default_scoping << construct_finder_arel(options, default_scoping.pop) end end