title: Rails, MongoID and embedded relations
body: <p>I have a few question about MongoID and embedded relations in Rails.</p>
  
  <p>In my model I have:</p>
  
  <pre><code>class Board
    include Mongoid::Document
  
    attr_accessible :title
  
    field :title, :type =&gt; String
  
    #has_and_belongs_to_many :users
    embeds_many :items
  end
  </code></pre>
  
  <p>when I call</p>
  
  <pre><code>Board.all
  </code></pre>
  
  <p>it returns the whole collection, including also :items.</p>
  
  <p>I've read in many articles/forums that using MongoDB embedded relations should be preferred over referenced ones but I have some questions:</p>
  
  <ul>
  <li>What about performaces? each time i want to retrieve a board i'll also retrieve items inside it: it may be useful sometimes but in the case i want only board's information and not items inside it I should create a new method for not retrieving items.</li>
  <li>When I want to update an item the DB will reload the whole document and not only the item I want to retrive, right?</li>
  </ul>
  
  <p>Up to now I've noticed that the only advantage in using embedded document is for something like what in SQL are called "joins" but I also see a lot of performaces problem, there are important reason to use embedded relations over referenced relations?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As pointed out by Adam C my thoughts are releated to situations like these:</p>
  
  <p>as explained before I will have Boards each one with many Items inside it and using Rails scaffolding it generates methods that retrieve the whole Board document from the database but many times (for example when editing a Board) i want to load the document without the Items part.</p>
  
  <p>Since I will be using mostly JSON calls my idea was to add an optional parameter to the url like "?get_items" to be set to TRUE in case I want also to get items, in other situations I would use Mongoid's:</p>
  
  <pre><code>Model.without
  </code></pre>
  
  <p>For example let's take the index action:</p>
  
  <pre><code>  def index
      @boards = Board.all
  
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @boards }
      end
    end
  </code></pre>
  
  <p>I'll need to get only fields specified in Board Model (in that case only :title) without items so I may use:</p>
  
  <pre><code>  def index
      @boards = Board.without :items
  
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @boards }
      end
    end
  </code></pre>
  
  <p>That my cause some problems?</p>