title: Thread safety of a class variable in Rails - will this work?
body: <p>I'm using the <a href="http://money.rubyforge.org/" rel="nofollow">Ruby Money gem</a> in a multi-tenant (SaaS) Rails app, and am looking for a good way to make the <code>Money.default_currency</code> be set to an Account's preference for each request. I have several currency-related models in the app that use the Money class.</p>
  
  <p>I have everything working properly in development, but I'm just looking for some feedback on whether or not the solution with have repercussions in production.</p>
  
  <p>Here's what I did in my ApplicationController (irrelevant code removed for brevity):</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    before_filter :set_currency
  
    private
  
    def set_currency
      Money.default_currency = Money::Currency.new(current_account.present? &amp;&amp; current_account.currency.present? ?
                                                   current_account.currency : 'USD')
    end
  end
  </code></pre>
  
  <p>So the code above will set the <code>default_currency</code> class variable to the current account's preference, or default back to 'USD' if there isn't one.</p>
  
  <p>By the way, here's the relevant <code>default_currency</code> code in the Money class:</p>
  
  <pre><code>class Money
  
    # Class Methods
    class &lt;&lt; self
  
      # The default currency, which is used when +Money.new+ is called without an
      # explicit currency argument. The default value is Currency.new("USD"). The
      # value must be a valid +Money::Currency+ instance.
      #
      # @return [Money::Currency]
      attr_accessor :default_currency
  
    end
  end
  </code></pre>
  
  <p><strong>So, will this work as expected in a multi-user setting? Anything else I need to do?</strong></p>