title: default_scope in Sequel
body: <p>In ActiveRecord there is a <code>default_scope</code> class method to specify a default scope. For example</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    default_scope where(:deleted =&gt; false)
  end
  
  User.all # =&gt; SELECT * FROM users WHERE deleted = 0;
  </code></pre>
  
  <p>How can I do this in <code>Sequel::Model</code>?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After some googling I eventually found some useful information.</p>
  
  <pre><code>class User &lt; Sequel::Model
  
    # Define some "scopes" (filters on the dataset)
    dataset_module do
      def existing
        filter(deleted: false)
      end
  
      def active
        filter(disabled: false)
      end
    end
  
    # This is the equivalent to a default_scope. Set one of the datasets
    # as the default dataset for this model.
    set_dataset(self.active)
  end
  </code></pre>
  
  <p>The generated query then looks like this:</p>
  
  <pre><code>User.all # =&gt; SELECT * FROM `users` WHERE (`deleted` IS FALSE)
  </code></pre>
  
  <p><i>By the way</i>: The equivalent to <code>unscoped</code> is <code>unfiltered</code>:</p>
  
  <pre><code>User.unfiltered.all # =&gt; SELECT * FROM `users`
  </code></pre>
  
  <p><strong>But</strong>, there is one problem. If you try to update a user you got from an unfiltered dataset, it tries to update the user using the given dataset.</p>
  
  <pre><code>User.create(disabled: true, deleted: true)
  User.all # =&gt; []
  u = User.unfiltered.first # =&gt; Given user
  u.disabled = false
  u.save # =&gt; UPDATE users SET ... WHERE (disabled IS FALSE AND id = 1)
  # =&gt; Sequel::NoExistingObject: Attempt to update object did not result in a single row modification
  </code></pre>
  
  <p><strong>So I am back at the beginning.</strong> Any workaround for this?</p>