title: Parameter in default scope in Model
body: <p>I am using Devise to authenticate users. I need to show/hide data based on global_location_id column in all my tables. The value of current global_location_id will come from current_user.global_location_id. I tried adding a following default scope to one of my models:</p>
  
  <pre><code>class Artifact &lt; ActiveRecord::Base
  default_scope where(:global_location_id =&gt; current_user.global_location_id)
  </code></pre>
  
  <p>But it gives following error:</p>
  
  <pre><code>undefined local variable or method `current_user' for #&lt;Class:0x8721840&gt;
  </code></pre>
  
  <p>To circumvent the unavailability of current_user in models I added following to my application controller</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
     def set_global_location_id
      @global_location_id = current_user.global_location_id
     end
  </code></pre>
  
  <p>And called the method from my controller</p>
  
  <pre><code>class ArtifactsController &lt; ApplicationController
     #Check to see if the user is logged in first
     before_filter :authenticate_user!
     before_filter :set_global_location_id
  </code></pre>
  
  <p>Changed the default scope in model to add @global_location_id</p>
  
  <pre><code>class Artifact &lt; ActiveRecord::Base
    default_scope where(:global_location_id =&gt; @global_location_id)
  </code></pre>
  
  <p>But this does not work either. The @global_location_id is being set properly in application controller. But it is empty in the model. How to make it work.</p>