0

「CEO」という役職が少なくとも 1 つあるすべての企業をプルしたいと考えています。

各テーブルと交差のクエリと一緒にハックすることができました(私は知っています...結合はありませんhttp://mongoid.org/en/mongoid/docs/tips.html#relational_associations、およびmongoid の N+1 問題、会社にポジションを埋め込むこともできます)が、次のようなことを行う方法は次のとおりです。

Company.includes(:positions).where("positions.title" => "CEO")?

ありがとう:

class Position
  include Mongoid::Document

  field :title, type: String
  field :profile_id, type: String
  field :tenure, type: BigDecimal

  belongs_to :company, index: true

class Company
  include Mongoid::Document

  field :name, type: String
  field :linkedin_id, type: String
  field :positions_count, type: Integer #Mongo Index

  belongs_to :industry, index: true
  has_many :positions

  index({ positions_count: 1}, {background: true})
4

1 に答える 1

1

N+1 問題を回避するには、Mongoid のidentity_map 機能を有効にします

これにより、次のクエリを実行できます。

companies_with_ceo = Position.where(title: 'CEO').includes(:company).map(&:company)

データベースに対して2つのクエリのみを実行する必要があります。

于 2012-12-06T20:45:35.567 に答える