Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

unscoped on a relation removes where clause for relation from scope

#5909

If you do model.relation.unscoped.all the where clause limiting the relations results gets removed by the unscoped call. I noticed this method isnt documented much so maybe this is how its suppose to work?

in this example Facility has default_scope order('name ASC') which i didn't want (need to order by something else in this one place) I did this reverse with Facility.unscoped.joins(:entity).where(...).order(...) and get the proper result i want. but this should be fixed, or documented on.

ruby-1.9.2-p0 > puts Corporation.first.facilities.scoped.to_sql
SELECT     "facilities".* FROM       "facilities"  WHERE     ("facilities".entity_id = 2) ORDER BY  name ASC

ruby-1.9.2-p0 > puts Corporation.first.facilities.unscoped.to_sql
SELECT     "facilities".* FROM       "facilities"

Reported by Zachery Hostens · November 2nd, 2010 @ 08:53 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. bterkuile
    bterkuile

    The best behaviour would be: Corporation.first.facilities.without_default_scope.to_sql => SELECT "facilities".* FROM "facilities" WHERE ("facilities".entity_id = 2)

    That would make this ticket a feature request.

    November 3rd, 2010 @ 02:39 PM

  2. Zachery Hostens
    Zachery Hostens
    • Tag changed from activerecord relation, arel, unscoped to activerecord relation, arel, rails3.0.1, unscoped

    this would be fine and dandy. except for that method does not exist. Note this is Rails 3.0.1

    ruby-1.9.2-p0 > Corporation.first.facilities.without_default_scope.to_sql
    NoMethodError: undefined method `without_default_scope' for #<Class:0x00000102a87980>
    
    ruby-1.9.2-p0 > Corporation.first.facilities.scoped.without_default_scope.to_sql
    NoMethodError: undefined method `without_default_scope' for #<ActiveRecord::Relation:0x000001014fbd00>
    

    November 4th, 2010 @ 05:07 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • Importance changed from to Low

    Zanchery, can you provide a patch or a failing test case?

    November 4th, 2010 @ 10:09 PM

  4. Zachery Hostens
    Zachery Hostens

    I will look at writing a test case or two and possibly a patch, though it might be a couple days. Im going out of town this weekend, but will try to get to it as soon as possible.

    November 4th, 2010 @ 10:14 PM

  5. Santiago Pastorino
  6. bterkuile
    bterkuile

    I created a fix for an issue specific to my problem where i manually removed scopes and add them later again when the rest of the query was build. This is not a nice solution, but it works. This method can be used to construct a without_default scope storing the default scopes and removing them from where_values, etc. The code for this is at:
    https://github.com/bterkuile/delete_softly/blob/master/lib/class_me...

    November 5th, 2010 @ 12:16 AM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from activerecord relation, arel, rails3.0.1, unscoped to activerecord relation, arel, rails301, unscoped

    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.

    February 5th, 2011 @ 08:56 PM

  8. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 5th, 2011 @ 08:56 PM