#1202 hold
Piotr Solnica (solnic)

Overwriting default_scope conditions is broken

Reported by Piotr Solnica (solnic) | February 24th, 2010 @ 06:30 AM

require 'rubygems'
require 'dm-core'
require 'spec'

DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup :default, "sqlite3::memory:"

class User
  include DataMapper::Resource
  property :id,     Serial
  property :active, Boolean

  default_scope(:default).update(:active => true)


describe "Overwriting default scope conditions" do
  before :all do
    @active   = User.create(:active => true)
    @inactive = User.create(:active => false)

  it "should overwrite a condition" do
    users = User.all(:active => false)

    users.size.should == 1
    users.first.should == @inactive

The produced query looks like this:

SELECT "id", "active" FROM "users" WHERE ("active" = 'f' AND "active" = 't') ORDER BY "id"

Comments and changes to this ticket

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) February 24th, 2010 @ 06:48 AM

    • Tag changed from default_scope bug to bug, default_scope
  • Marcin Kulik

    Marcin Kulik February 24th, 2010 @ 06:52 AM

    I can confirm that this issue is reproducible on 0.10.2 and on master branch.

  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) February 24th, 2010 @ 02:30 PM

    • State changed from “unconfirmed” to “confirmed”
    • Milestone set to 1.0.0
  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) February 24th, 2010 @ 03:26 PM

    • Assigned user set to “Piotr Solnica (solnic)”
  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) May 18th, 2010 @ 08:34 AM

    • Milestone cleared.
    • Tag changed from bug, default_scope to default_scope, suggestion
    • State changed from “confirmed” to “hold”
    • Assigned user cleared.

    It's a private API so the ticket should be treated more like a suggestion to make the default_scope a public method and of course make it work correctly.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 18th, 2010 @ 01:18 PM

    yeah, generally I name my private API methods as something a bit longer/uglier than I normally would for a public method. In this case what I'd probably do is make a "scope" method, mark it as public and make that part of the API.

    The other thing I would like to do with the scope behaviour is make it so that the default scope is used for property defaults. So like in the above case if you did:

    user = User.create
    user.active  # => true (rather than nil)

    This behaviour is consistent with how Collection works now, eg:

    # NOTE: assume there is no default scope
    user = User.all(:active => true).create
    user.active  # => true
  • Marcin Kulik

    Marcin Kulik May 18th, 2010 @ 01:51 PM

    With or without using it for property defaults, it's still VERY useful and my apps strongly rely on it. I'd love to see it in public API.

  • komagata (at gmail)

    komagata (at gmail) February 6th, 2011 @ 07:33 AM

    • Milestone order changed from “0” to “0”

    I like default_scope too.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »