This project is archived and is in readonly mode.
Merge default scopes by default
Reported by David Chelimsky | May 12th, 2010 @ 02:01 PM | in 3.0.2
I just co-authored a merged_default_scopes
gem (AR plugin) with Brian Tatnall. It modifies the behavior of
default_scope
so that it merges the arguments of
successive calls. We needed this in an app that was using a plugin
that used default_scope
and also needed to add to the
default scope in the consuming model.
We think this should be the default behavior.
If you agree, we'd be glad to submit a patch, including a
clear_default_scope
method so that anybody counting on
the current behavior to reset the default scope still has a way to
accomplish that.
Comments and changes to this ticket
-
David Chelimsky May 12th, 2010 @ 02:01 PM
- Assigned user changed from José Valim to Yehuda Katz (wycats)
-
David Chelimsky May 12th, 2010 @ 02:02 PM
- Tag set to activecord, default_scope
- Assigned user changed from Yehuda Katz (wycats) to Pratik
-
David Chelimsky May 12th, 2010 @ 02:04 PM
(sorry for the "Assigned user" spam - just changing that value to get this on the radar of more than one person)
-
José Valim May 12th, 2010 @ 05:26 PM
- 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 May 12th, 2010 @ 05:57 PM
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 May 15th, 2010 @ 08:51 AM
- 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... -
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to Low
-
bterkuile November 2nd, 2010 @ 09:23 AM
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 November 2nd, 2010 @ 09:28 AM
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
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Tags
Referenced by
- 4598 default_scope treats hashes and relations inconsistently when overwriting In working on #4583, Brian and I discovered some inconsis...
- 4598 default_scope treats hashes and relations inconsistently when overwriting Based on ticket #4583 I should assume that the desired be...
- 4583 Merge default scopes by default [#4583 state:resolved]