Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the default scope #7

Open
byhbt opened this issue Jul 2, 2020 · 0 comments
Open

Modify the default scope #7

byhbt opened this issue Jul 2, 2020 · 0 comments
Labels
Ruby Learning Ruby on Rails

Comments

@byhbt
Copy link
Owner

byhbt commented Jul 2, 2020

Problem:

Today I work with a model, which is defined default scope.
But i need to get the data which including the inactive status.

module SuperStore
  class StoreItem < ApplicationRecord
    # Enums
    enum status: [:inactive, :active], _suffix: true

    # Scopes
    default_scope { active_status.order(:type, updated_at: :desc) }
  end
end

Solution:

Override by adding this.

scope :with_inactive_status, -> { unscope(where: :status) }

Then use it as:

def set_resource
  @store_item = StoreItem.with_inactive_status.find(params[:id])
end

Lesson:

@byhbt byhbt added the Ruby Learning Ruby on Rails label Jul 2, 2020
@byhbt byhbt added this to the First 50 issues milestone Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ruby Learning Ruby on Rails
Projects
None yet
Development

No branches or pull requests

1 participant