title: Mongoid Model won't persist has_one via embeds_one relation
body: <p>I'm having trouble with embeds in a mongoid4-based rails 4 app. I've been looking for an answer everywhere for the past 2 days. So here is the code.</p>
  
  <p>This is a church management app, with a Service model, embedding a team and a band. Each team/band has several roles such as "presidence", "communion" that refer to a user.  </p>
  
  <p>My models :</p>
  
  <pre><code>class Service
    include Mongoid::Document
    ...
    embeds_one :team, autobuild: true
    embeds_one :band, autobuild: true
    ...
    accepts_nested_attributes_for :team, :band
  end
  
  class Team
    include Mongoid::Document  
  
    embedded_in :service
  
    has_one :presidence, :class_name =&gt; 'User', autosave: true
    has_one :message, :class_name =&gt; 'User', autosave: true
    ...
  end
  
  class Band
    include Mongoid::Document
    has_one :lead, :class_name =&gt; 'User', autosave: true
    has_one :guitar, :class_name =&gt; 'User', autosave: true
    ...
    embedded_in :service
  end
  
  class User
    include Mongoid::Document
    embeds_one :profile
  
    belongs_to :team, :inverse_of =&gt; :presidence
    belongs_to :team, :inverse_of =&gt; :message
  
    belongs_to :band, :inverse_of =&gt; :lead
    belongs_to :band, :inverse_of =&gt; :guitar
  
    def fullname
      "#{profile.firstname} #{profile.lastname}"
    end
  
    def self.find_by_talent(talent)
      self.where("profile.talents.name" =&gt; talent)
    end
  end
  </code></pre>
  
  <p>The services controller :</p>
  
  <pre><code># POST /services
  # POST /services.json
  def create
    @service = Service.new(service_params)
    respond_to do |format|
      if @service.save
        format.html { redirect_to @service, notice: 'Service was successfully created.' }
        format.json { render action: 'show', status: :created, location: @service }
      else
        format.html { render action: 'new' }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end
  
  ...
  
  def service_params
    params.require(:service).permit(:date, :time, :place, :name, :theme, :team =&gt; [:id, :precedence, :message ], :band =&gt; [:id, :lead, :guitar ])
  end
  </code></pre>
  
  <p>And the form in _form.html.erb :</p>
  
  <pre><code>&lt;%= form_for(@service) do |f| %&gt;
    ...
    &lt;%= f.fields_for @service.team  do |tf| %&gt;
      &lt;%= tf.collection_select :presidence, User.find_by_talent(:presidence), :_id, :fullname, {:include_blank =&gt; "select a person"} %&gt;
      &lt;%= tf.collection_select :message, User.find_by_talent(:message), :id, :fullname, {:include_blank =&gt; "select a person"} %&gt;
    &lt;% end %&gt;
    &lt;%= f.fields_for @service.band  do |bf| %&gt;
      &lt;%= bf.collection_select :lead, User.find_by_talent(:lead), :id, :fullname, {:include_blank =&gt; "select a person"} %&gt;
      &lt;%= bf.collection_select :guitar, User.find_by_talent(:guitar), :id, :fullname, {:include_blank =&gt; "select a person"} %&gt;
    &lt;% end %&gt;
    ...
  &lt;% end %&gt;
  </code></pre>
  
  <p>When creating a service, everything seems to run fine, but this is what I get in the console :</p>
  
  <pre><code>2.0.0-p195 :001 &gt; s = Service.last
   =&gt; #&lt;Service _id: 52ea18834d61631e7e020000, date: "2014-02-02", time: "10:00", place: "Where it's at", name: "My great name", theme: "The service's theme"&gt; 
  2.0.0-p195 :002 &gt; s.team
   =&gt; #&lt;Team _id: 52ea18834d61631e7e030000, &gt; 
  2.0.0-p195 :003 &gt; s.team.presidence
   =&gt; nil 
  </code></pre>
  
  <p>Why is s.team.presidence not created ? s.team looks weird, too, with an ending comma...</p>
  
  <p>Here is the content of my rails log :</p>
  
  <pre><code>Started POST "/services" for 127.0.0.1 at 2014-01-30 10:16:51 +0100
  Processing by ServicesController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"Ph6lbdHC2FbiANn/fGSzHWprenj3fWKXM40Hrsc5+AM=", "service"=&gt;{"date"=&gt;"2014-02-02", "name"=&gt;"My great name", "theme"=&gt;"The service's theme", "time"=&gt;"10:00", "place"=&gt;"Where it's at", "team"=&gt;{"presidence"=&gt;"52ea18324d61631e81010000", "message"=&gt;"52ea18324d61631e81010000"}, "band"=&gt;{"lead"=&gt;"52ea18324d61631e81010000", "guitar"=&gt;"52ea18324d61631e81010000"}}, "commit"=&gt;"Create Service"}
    MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=&gt;1} runtime: 0.6610ms
    MOPED: 127.0.0.1:27017 UPDATE       database=service_boot_camp_development collection=users selector={"band_id"=&gt;BSON::ObjectId('52ea18834d61631e7e010000'), "_id"=&gt;{"$nin"=&gt;[]}} update={"$set"=&gt;{"band_id"=&gt;nil}} flags=[:multi]
                           COMMAND      database=service_boot_camp_development command={:getlasterror=&gt;1, :w=&gt;1} runtime: 0.5800ms
    MOPED: 127.0.0.1:27017 INSERT       database=service_boot_camp_development collection=services documents=[{"_id"=&gt;BSON::ObjectId('52ea18834d61631e7e020000'), "date"=&gt;"2014-02-02", "time"=&gt;"10:00", "place"=&gt;"Where it's at", "name"=&gt;"My great name", "theme"=&gt;"The service's theme", "team"=&gt;{"_id"=&gt;BSON::ObjectId('52ea18834d61631e7e030000')}, "band"=&gt;{"_id"=&gt;BSON::ObjectId('52ea18834d61631e7e010000')}}] flags=[]
                           COMMAND      database=service_boot_camp_development command={:getlasterror=&gt;1, :w=&gt;1} runtime: 2.7460ms
  </code></pre>
  
  <p>I guess I'm doing something wrong, but I have no clue if it is in the database model or in the form... or anything else...</p>