title: Rails has_many / has_one same model
body: <p>So, I have two models, Collection and Folder.</p>
  
  <p>Within each Collection there is one root folder. Folders all belong to a Collection, but can also be nested within each other.</p>
  
  <p>Following <a href="https://stackoverflow.com/questions/2199049/rails-has-many-but-also-has-one-by-a-different-name">this question</a>, I wrote my models like shown below. I also added a callback, because I always want a Collection to start out with a root folder.</p>
  
  <pre><code>class Folder &lt; ActiveRecord::Base
    has_one :master_collection, :class_name =&gt; 'Collection', :foreign_key =&gt; :root_folder_id
    belongs_to :collection
    belongs_to :parent, :class_name =&gt; 'Folder'
    has_many :subfolders, :class_name =&gt; 'Folder', :foreign_key =&gt; :parent_id
    ...
  end
  
  class Collection &lt; ActiveRecord::Base
    belongs_to :root_folder, :class_name =&gt; 'Folder'
    has_many :folders
  
    after_create :setup_root_folder
  
    private
    def setup_root_folder
      self.root_folder = Folder.create(:name =&gt; 'Collection Root', :collection =&gt; self )
    end
  end
  </code></pre>
  
  <p>In Folders I have the columns: parent_id, folder_id
  In Collections I have the column: root_folder_id</p>
  
  <p>This seems like it should work, but I get this strange behavior in the console:</p>
  
  <pre><code>ruby-1.9.2-p0 :001 &gt; c = Collection.create(:name=&gt;"Example")
   =&gt; #&lt;Collection id: 6, name: "Example", ...timestamps..., root_folder_id: 8&gt; 
  ruby-1.9.2-p0 :003 &gt; f = c.root_folder
   =&gt; #&lt;Folder id: 8, parent_id: nil, name: "Collection Root", ...timestamps..., collection_id: 6&gt; 
  ruby-1.9.2-p0 :004 &gt; f.master_collection
   =&gt; nil 
  </code></pre>
  
  <p>So, clearly the association works on the collection side, but the root folder is not able to find the collection it is the root of, even though the foreign key is available and ActiveRecord isn't raising any errors on using the association...</p>
  
  <p>Any ideas?</p>