title: How do I use an in-memory object when working with :has_many and :belongs_to
body: <p>I am using Active Record with Rails 3. I have a class User that has_many Categories. I would like to instantiate categories from user that shares the same in-memory user instance. </p>
  
  <p>The default Active record behavior creates a new instance of user for each category. There is any way to change this behavior?</p>
  
  <p>Here are some snippets of my code. You can notice that I tried to use :inverse_of, without success...</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
    attr_accessor :name
    attr_accessible :name, :user
  
    belongs_to  :user, :inverse_of =&gt; :categories
  end
  
  class User &lt; ActiveRecord::Base
    attr_accessor :key
    attr_accessible :categories, :key
  
    has_many :categories, :inverse_of =&gt; :user
  end
  </code></pre>
  
  <p>I have the following spec to test the desired behavior: </p>
  
  <pre><code>user = User.first
  user.key = @key  # key is an :attr_accessor, not mapped to db
  
  category = user.categories.first
  category.user.key.should == user.key  # saddly, it fails.. :(
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Thank you for reading my question!</p>