title: Custom keys for Mongoid association
body: <p>I need to associate two models with a simple has_many. The problem is that I don't want to use the id (_id) as the primary key for the association. I still want the model to keep using the default ObjectIds for everything else.</p>
  
  <p>(This is running on Rails3.1 + Mongoid)</p>
  
  <p>So basically I want:</p>
  
  <pre><code>class Message
    ...
    field :message_id, :default =&gt; proc { "fail-#{Time.now.to_f.to_s}" }
    ...
    has_many :message_reports, primary_key: :message_id, foreign_key: :message_id
    ...
  end
  class MessageReport
    ...
    field :message_id, :default =&gt; proc { "fail-#{Time.now.to_f.to_s}" }
    ...
    has_many :message, primary_key: :message_id, foreign_key: :message_id
    ...
  end
  </code></pre>
  
  <p>This would only work for ActiveRecord. Mongoid don't support the primary_key option.</p>
  
  <p>So how do I get the same results for Mongoid collections?</p>
  
  <p>Before you say: don't do that...</p>
  
  <p>The reason I really really need to kay on this field and not the proper id is that these are messages... and the message_ids are unique ids returned by the API I call to send a message. Later the same id is received in callbacks from the other side.</p>
  
  <p>I could just do queries and stick it in a method to find the "associated" reports from a message and vice versa... I'd rather have them be actual associations, if possible.</p>
  
  <p>I could force the report-recieving process to search for and match up the objects for the association... but I'd rather not put that responsibility there when it is kind-of superfluous and it has nothing more to do with this data besides validating and saving it.</p>
  
  <p>In short: I'd prefer an association :)</p>