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 => "a string"

#2371

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.

Reported by Bruno Michel · March 29th, 2009 @ 09:52 PM

State: stale
Milestone: 2.3.10
Assigned to: Manfred Stienstra Manfred Stienstra
Importance: High

Activity

  1. Bruno Michel
    Bruno Michel

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

    March 30th, 2009 @ 10:56 PM

  2. Arnaud Berthomier
  3. Nick M
    Nick M

    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!

    April 7th, 2009 @ 11:12 PM

  4. Eloy Duran
    Eloy Duran
    • Assigned user set to Manfred Stienstra

    April 9th, 2009 @ 09:00 AM

  5. Steven Soroka
    Steven Soroka

    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.

    April 9th, 2009 @ 06:14 PM

  6. Bruno Michel
    Bruno Michel

    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.

    April 9th, 2009 @ 07:49 PM

  7. Manfred Stienstra
    Manfred Stienstra
    • State changed from new to open

    I'll include this in with "the big scopes rewrite".

    April 9th, 2009 @ 08:22 PM

  8. qoobaa
    qoobaa

    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
    

    June 7th, 2009 @ 01:02 PM

  9. qoobaa
    qoobaa

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

    June 7th, 2009 @ 01:05 PM

  10. Michael Koziarski
    Michael Koziarski
    • 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?

    June 9th, 2009 @ 09:17 AM

  11. Manfred Stienstra
    Manfred Stienstra

    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.

    June 9th, 2009 @ 09:30 AM

  12. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  13. Rizwan Reza
    Rizwan Reza
    • Tag changed from active_record, bug, default_scope, patch to active_record, bug, bugmash, default_scope, patch

    May 16th, 2010 @ 02:41 AM

  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.6 to 2.3.7

    May 23rd, 2010 @ 05:54 PM

  15. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.7 to 2.3.8

    May 24th, 2010 @ 09:40 AM

  16. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.8 to 2.3.9

    May 25th, 2010 @ 11:45 PM

  17. Paul Gideon Dann
    Paul Gideon Dann
    • 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?

    August 9th, 2010 @ 01:08 PM

  18. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  19. Santiago Pastorino
    Santiago Pastorino

    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.

    February 2nd, 2011 @ 04:32 PM

  20. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:32 PM