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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:05:13+00:00 2026-05-28T19:05:13+00:00

Are class variables a good way to set the context for a class instance

  • 0

Are class variables a good way to set the context for a class instance in a Rails application?

We are building a rails application that manages members of clubs. As such there is a Member model and a Club model. Since a member can belong many clubs and a club can have many members, there is a HABTM association between them.

The difficulty we are having is determining the club context in which a member instance exists. For example, to set a member dues status as “paid”, we need to know for which club the dues have been paid.We would like the controller to simply set something like

@member.paid=true  

without having to fuss about remembering to set the club every time we update a member.

The solution we have come up with sets a class variable in Member.

application_controller.rb
class ApplicationController < ActionController::Base

  before_filter :set_current_club

  def set_current_club
    Member.current_club = current_club
  end

  ...

end

As a somewhat contrived example, imagine the each club contains an array of members who have paid their dues. We would then have.

member.rb
class Member < ActiveRecord::Base

  cattr_accessor :current_club

  ...

  def paid=(status)
    if (status)
      @@current_club.paid_members << self
    else
      @@current_club.paid_member.delete(self) 
    end
  end

  def paid?
    @@current_club.paid_members.include?(self)
  end

  ...

end

The worry is that, by setting the current_club as a Member class variable in an ApplicationController before_filter, is it possible that there could be conflicts and race conditions when having more than one club accessing the application at the same time?

We did some debugging testing in our development environment, and showed that the @@current_club attribute in the model (Member.current_club) was always nil when entering the before_filter method in the ApplicationController. In other words, there was no evidence that current_club was affected by previous requests when serving a new request. So I think we are OK and that setting a model class variable from Application Controller is scoped to the user’s session. But I would like to be sure before going ahead with this approach.”

  • 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-28T19:05:14+00:00Added an answer on May 28, 2026 at 7:05 pm

    If you have the current_club property, which I assume is some form of a session variable, there is absolutely no need for the above code.

    To grab the Club record of a Member while having the current_club variable, simply iterate through all of the clubs associated with that member, find the record matching the current club and you’re done.

    Something like this:

    class Member < AR::Base
      def paid_for(club, status)
        if status
          club.paid_members << self
        else
          club.paid_members.delete(self)
        end
      end
    

    You don’t need to worry about race conditions, weirdness due to values hanging around and all that crap.

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

Sidebar

Related Questions

Is it good practice to let abstract classes define instance variables? public abstract class
While answering Static class variables in Python I noticed that PythonWin PyWin32 build 209.2
My guess is that class variables (class var) are truly global in storage (that
I currently have my PHP class variables set up like this: class someThing {
I noticed some code of a colleague today that initialized class variables in the
Whats a good way to set this up. I want to allow users to
ThisAddIn class created with new Outlook VSTO C# project has a Application property that
Can I put the painter into the class variables? : protected: QPainter *myPainter; ...
Is it better practice to use class variables, or the session cache in ASP.NET?
I find myself declaring bunch of class variables and it is really tiring to

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.