This project is archived and is in readonly mode.
Default Scope with conditions on create
Reported by Sam Granieri | March 9th, 2009 @ 04:30 PM | in 2.x
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?
Comments and changes to this ticket
-
Pratik March 9th, 2009 @ 04:45 PM
- Assigned user set to Pratik
Does the following work ?
default_scope :conditions=>{ :color => ["Red", "Blue"] }
-
Pratik March 9th, 2009 @ 04:47 PM
- Assigned user set to Pratik
- State changed from new to incomplete
Missing tests.
-
David Beckwith March 16th, 2009 @ 04:08 PM
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?
-
Peter Wagenet March 19th, 2009 @ 02:15 PM
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...
-
James Le Cuirot March 25th, 2009 @ 04:05 PM
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.
-
Pratik March 25th, 2009 @ 04:06 PM
- State changed from incomplete to open
-
James Le Cuirot March 25th, 2009 @ 04:22 PM
Sorry, the test wasn't quite right. Wasn't thinking straight.
-
Repository May 18th, 2009 @ 08:39 PM
- State changed from open to resolved
(from [97b75c9f1668992bd246fe46235c5764a4fa6e58]) Make sure default_scope#create checks for options[:conditions] [#2181 state:resolved] [James Le Cuirot] http://github.com/rails/rails/commit/97b75c9f1668992bd246fe46235c57...
-
Repository May 18th, 2009 @ 08:39 PM
(from [c5e109bbe766b0407a909361f18d2b87e9bfc75c]) Make sure default_scope#create checks for options[:conditions] [#2181 state:resolved] [James Le Cuirot] http://github.com/rails/rails/commit/c5e109bbe766b0407a909361f18d2b...
-
laiji July 20th, 2010 @ 03:13 AM
- 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
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
Attachments
Tags
Referenced by
- 2181 Default Scope with conditions on create (from [97b75c9f1668992bd246fe46235c5764a4fa6e58]) Make su...
- 2181 Default Scope with conditions on create (from [c5e109bbe766b0407a909361f18d2b87e9bfc75c]) Make su...
- 2654 [Patch] default_scope should ignore string conditions on new/create Looks like this is now fixed by #2181 97b75c9f1668992bd24...