Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:14:50+00:00 2026-06-08T14:14:50+00:00

In ActiveRecord there is a default_scope class method to specify a default scope. For

  • 0

In ActiveRecord there is a default_scope class method to specify a default scope. For example

class User < ActiveRecord::Base
  default_scope where(:deleted => false)
end

User.all # => SELECT * FROM users WHERE deleted = 0;

How can I do this in Sequel::Model?

EDIT:

After some googling I eventually found some useful information.

class User < 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

The generated query then looks like this:

User.all # => SELECT * FROM `users` WHERE (`deleted` IS FALSE)

By the way: The equivalent to unscoped is unfiltered:

User.unfiltered.all # => SELECT * FROM `users`

But, 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.

User.create(disabled: true, deleted: true)
User.all # => []
u = User.unfiltered.first # => Given user
u.disabled = false
u.save # => UPDATE users SET ... WHERE (disabled IS FALSE AND id = 1)
# => Sequel::NoExistingObject: Attempt to update object did not result in a single row modification

So I am back at the beginning. Any workaround for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:14:51+00:00Added an answer on June 8, 2026 at 2:14 pm

    The best workaround is avoiding the problem by not having a default scope. In most cases a default scope is a bad idea. If you want most of your queries to use a scope, then apply the scope manually in those queries, don’t use a default scope and try to backout the scope in other queries. A default scope only makes sense if all of your queries will use that scope.

    You can also handle this by subclassing (User is not scoped, ActiveUser < User is scoped). However, I find the explicit scoping approach works better.

    All that being said, if you really want to use a default scope, the following may work around the issue of updating a model instance outside the model’s default scope:

    User.instance_dataset.unfiltered!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an ActiveRecord::Base model with a default-scope: class Foo < ActiveRecord::Base default_scope
Is there any analog to ActiveRecord's find_by_ method in Datamapper ex: @user = User.find_by_email(params[:email])
models: #StatusMessage model class StatusMessage < ActiveRecord::Base belongs_to :users default_scope :order => created_at DESC
I have the following class Item < ActiveRecord::Base end class Talk < Item end
Ok so i have this data structure class User < ActiveRecord::Base has_many :companies, :through
add_column :users, :role, :string class User < ActiveRecord::Base attr_accessible :email, :password, :remember_me, :username devise
I have a model with a relationship like this: class Category < ActiveRecord::Base has_many
I'm using Rails 3. I have 3 models: class Deal < ActiveRecord::Base has_many :wishes,
Is there anything wrong with this (User model, line 95): scope :by_recent, where('created_at BETWEEN
I have a class similar to the following: class FruitKinds < ActiveRecord::Base Apple =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.