This project is archived and is in readonly mode.
Default scope with :conditions => "a string"
Reported by Bruno Michel | March 29th, 2009 @ 09:52 PM | in 2.3.10
When we use default_scope with :conditions => "a string" (or an array) in a model, we can't create new objects of this model. This small patch fixes this.
Comments and changes to this ticket
-
Bruno Michel March 30th, 2009 @ 10:56 PM
This patch also fixes the #2199 bug (with a test case to prove it).
-
Nick M April 7th, 2009 @ 11:12 PM
This same patch also fixes an "undefined method
merge'" error when trying to access a has_many relationship where the primary_key is specified and the associated model uses default_scope. If that doesn't make sense, I've attached another test case showing the problem that this fixes.
Thanks!
-
Eloy Duran April 9th, 2009 @ 09:00 AM
- Assigned user set to Manfred Stienstra
-
Steven Soroka April 9th, 2009 @ 06:14 PM
I had:
default_scope :conditions => ['available != ?', 1]
and this fixed the problem; however, I don't think default_scope should be assuming it can use the conditions, even if it is a hash, for default create options. What if my conditions said {:available => '!= 1'} or {:available => '> 1'} ? I believe those are valid, but still cannot be used as default create options.
Perhaps default_scope should be changed to take a :create parameter and use that or nothing.
-
Bruno Michel April 9th, 2009 @ 07:49 PM
I don"t think that {:available => '!= 1'} or {:available => '> 1'} are valid, but it can be a good idea to have a :create parameter when we want different conditions for creating objects and finding them. For example, the default status of an article can be 'draft' (not visible by default), but the default_scope uses {:status => 'public'} for listing only public articles.
-
Manfred Stienstra April 9th, 2009 @ 08:22 PM
- State changed from new to open
I'll include this in with "the big scopes rewrite".
-
qoobaa June 7th, 2009 @ 01:02 PM
It also fixes the bug with aggregate functions like count:
class Card < ActiveRecord::Base default_scope :conditions => "some string or array condition" end Card.count NoMethodError: undefined method `merge' for "some string or array condition":String
-
qoobaa June 7th, 2009 @ 01:05 PM
It also fixes the bug with aggregate functions like count:
class Card < ActiveRecord::Base default_scope :conditions => "some string or array condition" end Card.count # NoMethodError: undefined method merge' for [some string or array condition](String)
Sorry, Lighthouse understands only "ruby" in lowercase, can't edit previous post (that sucks).
-
Michael Koziarski June 9th, 2009 @ 09:17 AM
- Milestone changed from 2.x to 2.3.4
Manfred,
Seems like we should be able to get a fix for this into 2.3.4 in addition to master/3.0?
-
Manfred Stienstra June 9th, 2009 @ 09:30 AM
The scopes rewrite is based on 2.3, so in theory you could apply it to both. I wouldn't expect it to be fully finished really soon though.
-
Jeremy Kemper September 11th, 2009 @ 11:04 PM
- Milestone changed from 2.3.4 to 2.3.6
[milestone:id#50064 bulk edit command]
-
Rizwan Reza May 16th, 2010 @ 02:41 AM
- Tag changed from active_record, bug, default_scope, patch to active_record, bug, bugmash, default_scope, patch
-
Paul Gideon Dann August 9th, 2010 @ 01:08 PM
- Tag changed from active_record, bug, bugmash, default_scope, patch to arel rails3, active_record, bug, bugmash, default_scope, patch, rails3rc
- Importance changed from to High
I'm seeing this issue in Rails 3.0.0.rc when I set a default_scope to an "order" clause. It seems like a pretty big deal. Can this patch be forward-ported to Rails3?
-
Santiago Pastorino February 2nd, 2011 @ 04:32 PM
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 February 2nd, 2011 @ 04:32 PM
- State changed from open to stale
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
Referenced by
- 2199 default_scope Errors with named_scope duplicates #2371, mark as duplicate
- 2199 default_scope Errors with named_scope My patch in #2371 also fixes this bug.
- 2654 [Patch] default_scope should ignore string conditions on new/create Also #2371.