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.

Default Scope with conditions on create

#2181

I noticed something really weird with default scope.

In one scenario, imagine if you have a model named Car, with a string attribute called color.

By default, you'd want to search on blue or red cars

The class would look like this


class Car
  default_scope :conditions=>{:color=>"Red".."Blue"}
end

If you run this through a controller, you wont be able to create a new car.

Also, I've seen default scope complain if you pass in sql

Is this something that merits a patch?

Reported by Sam Granieri · March 9th, 2009 @ 04:30 PM

State: resolved
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Pratik

    Does the following work ?

    
    default_scope :conditions=>{ :color => ["Red", "Blue"] }
    

    March 9th, 2009 @ 04:45 PM

  2. Sam Granieri
    Sam Granieri
    • Assigned user cleared.

    Attached a patch for a proposed fix

    March 9th, 2009 @ 04:46 PM

  3. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to incomplete

    Missing tests.

    March 9th, 2009 @ 04:47 PM

  4. David Beckwith
    David Beckwith

    I noticed the same thing and it made me uneasy.

    
    class Event < ActiveRecord::Base
      default_scope :conditions => "id = 1"
    end
    
    Event.new
    
    
    NoMethodError: undefined method `id = 1=' for #<Event:0xb6dc2cf4>
    	from /home/david/Sites/azurrofashion.com/azurrofashion/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:255:in `method_missing'
    	from /home/david/Sites/azurrofashion.com/azurrofashion/vendor/rails/activerecord/lib/active_record/base.rb:2440:in `send'
    	from /home/david/Sites/azurrofashion.com/azurrofashion/vendor/rails/activerecord/lib/active_record/base.rb:2440:in `initialize'
    	from /home/david/Sites/azurrofashion.com/azurrofashion/vendor/rails/activerecord/lib/active_record/base.rb:2440:in `each'
    	from /home/david/Sites/azurrofashion.com/azurrofashion/vendor/rails/activerecord/lib/active_record/base.rb:2440:in `initialize'
    	from (irb):3:in `new'
    	from (irb):3
    

    Is this the same issue?

    March 16th, 2009 @ 04:08 PM

  5. Peter Wagenet
    Peter Wagenet

    Default_scopes do not currently behave the same as named_scopes which is weird. I've added a patch that helps to improve consistency. It may be able to help with your problem. http://rails.lighthouseapp.com/p...

    March 19th, 2009 @ 02:15 PM

  6. James Le Cuirot
    James Le Cuirot

    Regarding the example posted by David, this is happening because default_scope should be checking whether :conditions is a hash. Instead it is checking whether options is a hash, as well as merely checking whether :conditions exists. The check on options seems pointless as it would always be a hash, right?

    I came up with the following patch but it got me wondering what named_scope does about this. It turns out named_scope.rb does exactly the same thing as my patch! I'm not sure why default_scope was done any differently.

    March 25th, 2009 @ 04:05 PM

  7. Pratik
    Pratik
    • State changed from incomplete to open

    March 25th, 2009 @ 04:06 PM

  8. James Le Cuirot
    James Le Cuirot

    Sorry, the test wasn't quite right. Wasn't thinking straight.

    March 25th, 2009 @ 04:22 PM

  9. Repository
    Repository
    • State changed from open to resolved

    (from [97b75c9f1668992bd246fe46235c5764a4fa6e58]) Make sure default_scope#create checks for options[:conditions] [#2181 Default Scope with conditions on create state:resolved] [James Le Cuirot] http://github.com/rails/rails/commit/97b75c9f1668992bd246fe46235c57...

    May 18th, 2009 @ 08:39 PM

  10. Repository
  11. laiji
    laiji
    • Importance changed from to

    t money tiffany the Internet, but weight Pandora Beads international methodical modeling. "Minimalist" tiffanys may produce one shot Tiffany Charm access to construe it, Gucci jewelry but due to mask tiffany and co antithetic self-made womanliness be tiffany co entertained Pamela Anderson also tiffany Cindy Crawford, Tila remembers discount Chanel jewelry that the jewelry charge Christian Dior jewelry nourish to accentuate one's charact

    July 20th, 2010 @ 03:13 AM