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 3798662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:36:30+00:00 2026-05-19T13:36:30+00:00

I have a default scope on products due to information security constraints. class Product

  • 0

I have a default scope on products due to information security constraints.

class Product < ActiveRecord::Base
  has_many :photos

  default_scope where('visible = 1')
end

In my associated Photo model, however, I also have to find products that should not be visible.

class Photo < ActiveRecord::Base
  belongs_to :product
end

my_photo.product

In other cases, I can use unscoped in order to bypass the default_scope, e.g. in Product.unscoped.find_by_title('abc'). However:

How to remove the scope when using associations of a record?

my_photo.unscoped.product does not make sense as my_photo does not have a method called unscoped. Neither does my_photo.product.unscoped make sense as my_photo.product may already be nil.

  • 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-05-19T13:36:31+00:00Added an answer on May 19, 2026 at 1:36 pm

    Oh. I fooled myself. Thought the following would not work… but it does:

    Product.unscoped do
      my_photo.product
    end
    

    Notice that you have to call unscoped on the model with the default_scope that should be bypassed.

    Also, inheritance has to be respected. If you have class InsuranceProduct < Productand class FinancialProduct < Product and a default_scope in Product, all of the following two combinations will work:

    InsuranceProduct.unscoped do
      my_record.insurance_products
    end
    
    FinancialProduct.unscoped do
      my_record.financial_products
    end
    
    Product.unscoped do
      my_record.products
    end
    

    However, the following will not work although the scope is defined in Product:

    Product.unscoped do
      my_record.financial_products
    end
    

    I guess that’s another quirk of STI in Ruby / Rails.

    • 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
I have two models: Product and ProductType: Product: class Product < ActiveRecord::Base default_scope :order
I have following models: class Category < ActiveRecord::Base has_many :items default_scope where(:enabled => true,
I have a simple models class Item < ActiveRecord::Base has_many :parts end class Part
I have a table: <table class=products> <thead>...</thead> <tfoot>...</tfoot> <tbody id=the-list> <tr id=order-0> <th scope=row
I have a Position model for which I have a scope defined: scope :default,
I have a default scope set in my model. However, when I call a
I have a class that has no default constructor or assignment operator so it
I have a rails model with the following scopes: default_scope order('created_at ASC') scope :published,
I have Default.aspx page, which inherits from BasePage.cs, which inherits from System.Web.UI.Page. BasePage is

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.