title: Rails Model - attr_accessor raise unknown method exception?
body: <h2>The short version (dummy code):</h2>
  
  <p>So I have one ActiveRecord (rails model) class:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
      attr_accessor :somevar
  end
  </code></pre>
  
  <p>When I do </p>
  
  <pre><code>@u=User.find(1)
  @u.somevar = 1
  </code></pre>
  
  <p>I get </p>
  
  <pre><code>undefined method `somevar=' for #&lt;Class:0x007fa8cd0016a8&gt;
  </code></pre>
  
  <p>I do not have a column in the database called <code>somevar</code><br>
  I have restarted <code>rails server</code> after adding <code>attr_accessor</code> (just in case)<br>
  Still getting the bloody error.<br>
  Have googled <em>alot!</em></p>
  
  <p>Where can the error be? Any thoughts?</p>
  
  <p>I appriciate all answers! Thank you!</p>
  
  <hr>
  
  <h2>The long version (with the actual code)</h2>
  
  <p>I use Devise to manage my users.
  Also I'm trying to add a default condition, to filter the results, on some of the models. This is based on 'company_id' which is in the User model. I tried to use the session variable in <code>default_scope</code> and found <a href="https://stackoverflow.com/questions/10680845/owner-filtered-model-objects-on-rails-3">this</a>. It basically says that it is bad practice to use session vars to default conditions, and I could use Devise and add some modifications.</p>
  
  <p>This resulted in my <code>User model</code> to be</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    attr_accessor :current_company
  
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
    belongs_to :company
  end
  </code></pre>
  
  <p>And my <code>ApplicationController</code></p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    around_filter :scope_current_user  
  
    def scope_current_user
          User.current_company = current_user.company_id
      yield
      ensure
          #avoids issues when an exception is raised, to clear the current_id
          User.current_company = nil       
    end
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception
  
  end
  </code></pre>
  
  <p>Then it raises, from the <code>ApplicationController</code></p>
  
  <pre><code>undefined method `current_company=' for #&lt;Class:0x007fa8cd0016a8&gt;
  </code></pre>
  
  <p>The same happens if I define the methods manually:</p>
  
  <pre><code>def current_company
      @current_company
  end
  def current_company=(new_val)
      @current_company = new_val
  end
  </code></pre>