title: How to avoid race conditions when setting class variables on each request in Ruby on Rails
body: <p>I have an active resource like model that communicates with a restful resource.
  The resource path has some dynamic parameters so I'm setting some class variables on the model before each request.</p>
  
  <p>I have something like this:</p>
  
  <pre><code>class MyClass &lt;&lt; MySuperClass::Base
  
    class &lt;&lt; self
      attr_accessor :site
      attr_accessor :shop_id
      attr_accessor :product_id
  
      def get
        RestClient.get(self.site)
      end
  
      def set_site(shop_id, product_id)
        self.site = "http://example.com/api/shop/#{shop_id}/product/#{product_id}
      end
    end
  end
  </code></pre>
  
  <p>In my application controller I have a before filter that sets the shop_id and product_id</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    before_filter :set_site
  
    private
  
    def set_site
      MyClass.set_site(current_shop.id, current_product.id)
    end
  end
  </code></pre>
  
  <p>As I understand from here: <a href="http://m.onkey.org/thread-safety-for-your-rails" rel="nofollow">http://m.onkey.org/thread-safety-for-your-rails</a>
  This could be the cause of some race conditions.</p>
  
  <p>That article was written 3 years ago so it is still the case that setting class variables per request could cause a race condition?</p>
  
  <p>And if so then what is the current best practice to achieve similar behavior without incurring a race condition?</p>