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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:47:16+00:00 2026-05-26T04:47:16+00:00

I have two models, Forums and Topics, Forums has many Topics. Each has an

  • 0

I have two models, Forums and Topics, Forums has many Topics. Each has an active boolean. When I flip the active flag on a Forum, I want its Topics to all get their flag flipped as well. My thought was to do this in a before_save
def before_save
unless self.active?
self.topics.each{|topic| topic.close}
end

In Topic, I have defined the close method:
def close
self.active = false
self.save
end

Am I taking the wrong approach here (should I be doing is elsewhere, like in a controller?) I don’t get any errors, but nothing happens when I set the flag to false (I don’t necessarily want to flip all of the topics to active when setting the forum to active, so I only need this to go one way).

Thanks

  • 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-26T04:47:17+00:00Added an answer on May 26, 2026 at 4:47 am

    Nope, you’ve got the right approach, you just need a slight nudge. See example below.

    class Forum < ActiveRecord::Base
      has_many :topics
      after_save :close_topics!
    
      def close!
        self.active = false
        self.save!
      end
    
      private
    
      def close_topics!
        self.topics.each(&:close!) unless self.active
      end
    end
    
    class Topic < ActiveRecord::Base
      belongs_to :forum
    
      def close!
        self.active = false
        self.save!
      end
    end
    

    You can also use an ActiveRecord Observer. I personally prefer this over a filter as you are decoupling your classes more this way. And I tend to like proper separation of concerns, so..

    # rails generate observer Forum
    
    class ForumObserver < ActiveRecord::Observer
    
      def after_save (forum)
        forum.topics.each(&:close!) unless forum.active
      end
    
    end
    

    I’m assuming once these records are deactivated, they will not become active ever again. Regardless, however, the code for additional functionality to handle the opposite case is negligible.

    P.S. If you’re not familiar with the &:handler syntax, it’s simply a shortcut for mapping to function or variable per item in an enumerable.

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

Sidebar

Related Questions

I have two models: Show and Venue . Show has one venue while each
I have two models, Article and Post that both inherit from a base model
I have two models. We'll call them object A and object B. Their design
I have two models indexed for searching (User and Item). I'm trying to do
I have two models, Room and Image . Image is a generic model that
I have two models: Person and Relation. The second one stores information about relations
I have two models in my Django application, for the purposes of storing search
I have two models, A and B, and one light, L. I would like
I have two models, user and group. I also have a joining table groups_users.
I have two models, say Product and Bundle. products table is related to product_prices

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.