{"id":7909,"date":"2015-11-07T23:57:46","date_gmt":"2015-11-07T23:57:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/07\/how-to-implement-has_many-through-relationships-with-mongoid-and-mongodb-open-source-projects-mongoid-mongoid\/"},"modified":"2015-11-07T23:57:46","modified_gmt":"2015-11-07T23:57:46","slug":"how-to-implement-has_many-through-relationships-with-mongoid-and-mongodb-open-source-projects-mongoid-mongoid","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/07\/how-to-implement-has_many-through-relationships-with-mongoid-and-mongodb-open-source-projects-mongoid-mongoid\/","title":{"rendered":"How to implement has_many :through relationships with Mongoid and mongodb?-open source projects mongoid\/mongoid"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0ac3313bc5c901c756b38b209c63d45c?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>franciscodelgadodev<\/strong><\/p>\n<p>Steven Soroka solution is really great! I don&#8217;t have the reputation to comment an answer(That&#8217;s why I&#8217;m adding a new answer :P) but I think using map for a relationship is expensive(specially if your has_many relationship have hunders|thousands of records) because it gets the data from database, build each record, generates the original array and then iterates over the original array to build a new one with the values from the given block.<\/p>\n<p>Using pluck is faster and maybe the fastest option.<\/p>\n<pre><code>class Physician\n  include Mongoid::Document\n  has_many :appointments\n\n  def patients\n    Patient.in(id: appointments.pluck(:patient_id))\n  end\nend\n\nclass Appointment\n  include Mongoid::Document\n  belongs_to :physician\n  belongs_to :patient \nend\n\nclass Patient\n  include Mongoid::Document\n  has_many :appointments \n\n  def physicians\n    Physician.in(id: appointments.pluck(:physician_id))\n  end\nend\n<\/code><\/pre>\n<p>Here some stats with Benchmark.measure:<\/p>\n<pre><code>&gt; Benchmark.measure { physician.appointments.map(&amp;:patient_id) }\n =&gt; # \n\n&gt; Benchmark.measure { physician.appointments.pluck(:patient_id) }\n =&gt; # \n<\/code><\/pre>\n<p>I am using just 250 appointments. Don&#8217;t forget to add indexes to :patient_id and :physician_id in Appointment document!<\/p>\n<p>I hope it helps, Thanks for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>franciscodelgadodev Steven Soroka solution is really great! I don&#8217;t have the reputation to comment an answer(That&#8217;s why I&#8217;m adding a new answer :P) but I think using map for a relationship is expensive(specially if your has_many relationship have hunders|thousands of records) because it gets the data from database, build each record, generates the original array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7909","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7909"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7909\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}