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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:54:38+00:00 2026-05-24T08:54:38+00:00

I’ve been knocking my head against a problem for ages until I realised what

  • 0

I’ve been knocking my head against a problem for ages until I realised what was going on.

I want to have a committee which must always have at least one member. To achieve this, each member checks it’s not the last member before being destroyed.

The code below should prevent the last member being destroyed UNLESS the committee itself is being destroyed in which case it happily self-destructs.

class Committee < ActiveRecord::Base

  has_many :committee_members
  before_destroy: { @destroy_initiated = true }

  def destroy_initiated?
    @destroy_initiated
  end

end

class CommitteeMember < ActiveRecord::Base

  belongs_to :committee
  before_destroy :ensure_not_last

  def ensure_not_last
    unless self.committee.destroy_initiated?
      if self.committee.committee_members.count == 1
        raise 'You cannot remove the last committe member. Try destroying the committee instead'
      end
    end
  end

end

The problem

The problem is that each CommitteeMember references a different instance of the Committee object, they all have different object identities:

e.g. #<Committee:0x00000105c41f20> v. #<Committee:0x00000105c2c3a0>

This means that even when I set @destroy_initiated to be true on once instance of Committee with ID 20, it’s not going to be set to true on the instance referenced by one of its committee_members.

Leaving aside Rails 3.1 which I know has an identity map, is there a clean workaround to having an instance variable which is available on all instantiations of Committee?

I’m considering doing a class variable containing a map of destroy_initiated? to each Committee ID but this feels pretty messy.

  • 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-24T08:54:39+00:00Added an answer on May 24, 2026 at 8:54 am

    I’m not sure if I can answer this question properly without more context, but I can give you a possible solution to think about…

    In an app I just recently put in production, we would essentially cache an object as an instance variable on ApplicationController. Then whenever we needed it, we simply ask for the instance variable rather than finding it with Active Record.

    class ApplicationController < ActionController::Base
      before_filter :set_committee
    
      def set_committee
        @committee ||= Committee.new()
      end
    end
    

    So now, for the duration of the request, anything inheriting from ApplicationController can access the @committee object. If you can use a similar pattern (doesn’t have to be application controller, could just be any other controller) you would essentially have a “global” variable for the duration of the request.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

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.