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

  • Home
  • SEARCH
  • 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 9188797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:05:59+00:00 2026-06-17T20:05:59+00:00

I have several objects that all have an approved field. What would be the

  • 0

I have several objects that all have an approved field.

What would be the best way to implement a scope to use across all models?

For example, I have a sighting object and a comment object. They both have to be approved by an admin before being availalbe to the public.

So how can I create a scope that returns comment.approved as well as sighting.approved respectively without repeating it on each model? Is this where concerns comes into play?

  • 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-17T20:06:00+00:00Added an answer on June 17, 2026 at 8:06 pm

    While just declaring a scope in each model is fine if you just want the scoping functionality. Using an ActiveSupport::Concern will give you the ability to add additional methods as well if that’s something you think is going to happen. Here’s an example:

    # /app/models/concerns/approved.rb
    module Approved
      extend ActiveSupport::Concern
    
      included do
        default_scope { where(approved: false) }
        scope :approved, -> { where(approved: true) }
      end
    
      def unapprove
        update_attribute :approved, false
      end
    end
    
    class Sighting < ActiveRecord::Base
      include Approved
    end
    
    class Comment < ActiveRecord::Base
      include Approved
    end
    

    Then you can make calls like Sighting.approved and Comment.approved to get the appropriate list of approved records. You also get the unapprove method and can do something like Comment.approved.first.unapprove.

    In this example, I’ve also included default_scope which will mean that calls like Sighting.all or Comment.all will return only unapproved items. I included this just as an example, it may not be applicable to your implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several templated objects that all implement the same interface: I.E. MyObject<datatype1> obj1;
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I have a class that is called regularly by several objects. I would like
I have several classes (A, B, C, ...) that all use a List<AnotherClass> to
I have a class that creates several IDisposable objects, all of these objects are
I have several objects in my app that can become nil at some point
I have users that have several objects and can upload images for those objects.
I have several methods that populate a SQLCommand objects parameter collection from an object
I have a mutable array that is retained and storing several objects. At some
I'm trying to put together a form_tag that edits several Shift objects. I have

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.