0

私はグループモデルを持っています。グループにクエリを実行して、ユーザーがグループに存在するかどうかを確認したいと考えています。通常、これは埋め込みドキュメントを使用する方がはるかに簡単ですが、残念ながらこの場合はできません。組み込みシナリオでは、次のことを行います。参照されたシナリオでこのクエリを実行するにはどうすればよいですか。

注:** habtm関係は使用したくありません。

クエリ

Matter.where(:'matter_counsels._id' => the_id)

クラス

class Matter
  include Mongoid::Document

  # Relationships
  has_many :matter_counsels # subclass of MatterRelationship
  has_many :matter_clients # subclass of MatterRelationship
  has_many :matter_opposing_parties # subclass of MatterRelationship
  has_many :matter_related_parties # subclass of MatterRelationship

end


class MatterRelationship
  include Mongoid::Document

  belongs_to :matter
end
4

1 に答える 1

0

上記の例が明確ではなかったことを認めなければなりません。解決策とともに問題を更新しました。

class Matter
  include Mongoid::Document

  # Relationships
  has_many :matter_counsels # subclass of MatterRelationship
  has_many :matter_clients # subclass of MatterRelationship
  has_many :matter_opposing_parties # subclass of MatterRelationship
  has_many :matter_related_parties # subclass of MatterRelationship

  # Finder Scopes
  class << self

    # finds a type of matter relationship by user
    def for_user(user,type)
      user_id = user.id.to_s
      matter_ids = MatterRelationship.where(contact_id: user_id, _type: type).collect{ |i| i.matter_id.to_s }
      where(:_id.in => matter_ids)
    end

  end

end
于 2012-05-10T01:53:36.860 に答える