This project is archived and is in readonly mode.

#5909 ✓stale
Zachery Hostens

unscoped on a relation removes where clause for relation from scope

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

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"

Comments and changes to this ticket

  • bterkuile

    bterkuile November 3rd, 2010 @ 02:39 PM

    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.

  • Zachery Hostens

    Zachery Hostens November 4th, 2010 @ 05:07 PM

    • 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>
    
  • Santiago Pastorino

    Santiago Pastorino November 4th, 2010 @ 10:09 PM

    • Importance changed from “” to “Low”

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

  • Zachery Hostens

    Zachery Hostens November 4th, 2010 @ 10:14 PM

    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.

  • Santiago Pastorino
  • bterkuile

    bterkuile November 5th, 2010 @ 12:16 AM

    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...

  • Santiago Pastorino

    Santiago Pastorino February 5th, 2011 @ 08:56 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 5th, 2011 @ 08:56 PM

    • State changed from “open” to “stale”
  • klkk

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

Pages