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 回答