title: Create mongoid model with references
body: <p>I want to create model which is referenced to another:</p>
  
  <pre><code>class User  
    include Mongoid::Document  
    field :name, :type =&gt; String
    belongs_to :post
  end 
  
  class Post
    include Mongoid::Document  
    field :content, :type =&gt; String  
    has_one :author, :class_name =&gt; 'User'
  end
  </code></pre>
  
  <p>But I am confused how to make mongoDB work accordingly to rules on model files (user.rb, post.rb).</p>
  
  <p>First of all I have created model throw command line:</p>
  
  <pre><code>rails generate model User name:string
  rails generate model Post content:string
  </code></pre>
  
  <p>Then I manually edited model files. I have added this line</p>
  
  <pre><code>belongs_to :post
  has_one :author, :class_name =&gt; 'User'
  </code></pre>
  
  <p>Then I run code in one action:</p>
  
  <pre><code>post = Post.new
  post.content = "text"
  post.author = User.new
  post.save!
  </code></pre>
  
  <p>As result in database I see only content field. No author field.</p>
  
  <p>What should I do and what I am doing wrong?</p>
  
  <p>[ANSWER]
  I confused has_to and belongs_to places. So the correct models look like:</p>
  
  <pre><code>class User  
    include Mongoid::Document  
    field :name, :type =&gt; String
    has_many :post
  end 
  
  class Post
    include Mongoid::Document  
    field :content, :type =&gt; String  
    belongs_to :author, :class_name =&gt; 'User'
  end
  </code></pre>
  
  <p>Everything else keep as in question.</p>