This project is archived and is in readonly mode.

#2371 ✓stale
Bruno Michel

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

    Bruno Michel March 30th, 2009 @ 10:56 PM

    This patch also fixes the #2199 bug (with a test case to prove it).

  • Arnaud Berthomier
  • Nick M

    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

    Eloy Duran April 9th, 2009 @ 09:00 AM

    • Assigned user set to “Manfred Stienstra”
  • Steven Soroka

    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

    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

    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

    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

    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

    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

    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

    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

    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
  • Jeremy Kemper

    Jeremy Kemper May 23rd, 2010 @ 05:54 PM

    • Milestone changed from 2.3.6 to 2.3.7
  • Jeremy Kemper

    Jeremy Kemper May 24th, 2010 @ 09:40 AM

    • Milestone changed from 2.3.7 to 2.3.8
  • Jeremy Kemper

    Jeremy Kemper May 25th, 2010 @ 11:45 PM

    • Milestone changed from 2.3.8 to 2.3.9
  • Paul Gideon Dann

    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?

  • Jeremy Kemper

    Jeremy Kemper August 30th, 2010 @ 02:28 AM

    • Milestone changed from 2.3.9 to 2.3.10
  • Santiago Pastorino

    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

    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>

Referenced by

Pages