This project is archived and is in readonly mode.
AR::Base#unscoped inconsistency
-
Neeraj Singh
- Milestone set to 3.x
- State changed from new to open
- Importance changed from to Low
Yes it is a bug.
-
Neeraj Singh
I personally think a design change is needed to fix this issue. In the meantime following code will solve the issue.
Post.unscoped do self.written_by('dhh').to_sql end -
Cyrille
Moreover:
class Organization < ActiveRecord::Base default_scope where("organizations.state = 'active'").order("organizations.qs DESC, organizations.title ASC") unscoped do scope :latest, lambda { |num| order("organizations.created_at DESC").where("organizations.state = 'active'").limit(num) } end def self.latest2(num) unscoped.order("organizations.created_at DESC").where("organizations.state = 'active'").limit(num) end endruby-1.9.2-p0 > Organization.latest(5).to_sql
=> "SELECTorganizations.* FROMorganizationsWHERE (organizations.state = 'active') ORDER BY organizations.qs DESC, organizations.title ASC, organizations.created_at DESC LIMIT 5"if the named scope is defined inside the unscoped block it's not working. Whereas the following is working as excepted
ruby-1.9.2-p0 > Organization.latest2(5).to_sql
=> "SELECTorganizations.* FROMorganizationsWHERE (organizations.state = 'active') ORDER BY organizations.created_at DESC LIMIT 5" -
Neeraj Singh
- Assigned user set to Neeraj Singh
@Cyrille Personally I think latest is working as expected. The latest scope should not be declared inside unscoped.
I will take another look at this defect but as I remember from my first stab at this issue it is kind of hard to fix without some design change. However now that rails2 is released I will look into it with the possibility of some change in design.
-
Cyrille
@Neeraj
In rails 2 I think you were allowed to do stuff like
with_exclusive_scope do named_scope :most_recent ... named_scope :cheapest ... endIn order to define all named scope that don't require default_scope
So I was wondering what's the equivalent in Rails 3 as you said it's not unscoped do ?
The only way for now I find in order to defined scope that wouldn't apply default_scope is to create class method for them like my previous latest2 example. I just think with_exclusive_scope was DRYer ;)
-
Neeraj Singh
I posted the answer earlier in a comment.
-
Santiago Pastorino
- Milestone changed from 3.x to 3.1
- Assigned user changed from Neeraj Singh to Aaron Patterson
-
rails
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
rails
- State changed from open to stale
-
Brian Bokor
I dont' have a solution for this issue but I have noticed that in 3.0.5
Doesn't work
scope :latest, unscoped.order()Doesn't work
unscoped doscope :latest, order()end
Works
def self.latestunscoped.order()end
Works (although deprecated)
scope :latest, reorder()I'm not sure I'll have time right now to look into fixing but that is what my tests have proven.