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

The Archive Base Latest Questions

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

I have several classes associated as following: class User < ActiveRecord::Base has_many :posts, :dependent

  • 0

I have several classes associated as following:

class User < ActiveRecord::Base
   has_many :posts, :dependent => :destroy

class Group < ActiveRecord::Base
   has_many :posts, :dependent => :destroy

class Post < ActiveRecord::Base
   belongs_to :user
   belongs_to :groups

Now, I want to delete Users, Groups, and Posts by setting their ‘active’ field to false and leave them in database. Initially, I was hoping that destroying a user or a group will cause destruction of their posts (delete action would set the flag to false), However as far as I understood, in such cases Ruby just breaks the corresponding association without destroying the posts. I am wondering whether it can be done nicely, or I just have to write a separate function and call it instead of regular delete?

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

    Rather than deleting records from the database, we can retire them with just a little bit of code and configuration.

    1. Create a new directory under your app/ directory called “concerns”.

    2. Create a new file in this directory called “retireable.rb”.

    3. Copy and paste the following code into it:

      module Retireable
        extend ActiveSupport::Concern
      
        included do
          default_scope   :conditions => { :retired => false }
          scope :retired, :conditions => { :retired => true }
        end
      
        def destroy
          retire
        end
        def destroy!
          raise StandardError, "Cannot destroy!"
        end
      
        def retire
          update_attribute :retired,    true
          update_attribute :retired_at, Time.now
        end
        def retired?
          retired
        end
      end
      

    4) Each of your models which need this functionality must:

    4a) Have the ‘retired‘ boolean field and ‘retired_at‘ datetime field in the database schema.

    4b) include Retireable must be within the class definition for that model; i.e.

        class User < ActiveRecord::Base
          include Retireable
          ...
    

    5) In your ‘config/application.rb‘, add the following line:

        config.autoload_paths << "#{Rails.root}/app/concerns"
    

    Now calls to instance.destroy (eg. @user.destroy, @group.destroy) will be handled by retireable, therefore not deleted from the database and not breaking the association.

    Hope that helps!

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

Sidebar

Related Questions

I have several classes like in the following example: abstract class AClass { boolean
I have several classes that are basically interfaces to database rows. Since the class
I have several classes from 3rd party library similar to the class, StagingConfigDatabase, which
I have a class CardStack . I have several classes that inherit from CardStack
I've noticed these two interfaces, and several associated classes, have been added in .NET
I have several classes (for a Form generator class thing I'm making...I know sad..
If you have several classes where you want them to inherit from a base
I have several classes that conceptually belong to one tier. They have no common
i have several classes with members called 'Id'. Originally i wanted to store these
I have several classes, that all derives from SuperClass. When the classes are created,

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.