Note that there are some explanatory texts on larger screens.

plurals
  1. POquery embedded document matching several attributes
    primarykey
    data
    text
    <p>I have the following structure:</p> <pre><code>class User include Mongoid::Document end class Resource include Mongoid::Document embeds_many :permissions end class Permission include Mongoid::Document embedded_in :resource field :read, type: Boolean field :write, type: Boolean field :user_id, type: BSON::ObjectId end </code></pre> <p>Now let's suppose I have the following data:</p> <pre><code>1.9.3p194 :001 &gt; a = User.create =&gt; #&lt;User _id: 4ff46818f83222daf9000001, _type: nil&gt; 1.9.3p194 :002 &gt; b = User.create =&gt; #&lt;User _id: 4ff4681bf83222daf9000002, _type: nil&gt; 1.9.3p194 :003 &gt; r = Resource.create =&gt; #&lt;Resource _id: 4ff46822f83222daf9000003, _type: nil&gt; 1.9.3p194 :004 &gt; r.permissions.create(read: true, user_id: a.id) =&gt; #&lt;Permission _id: 4ff46835f83222daf9000004, _type: nil, read: true, write: nil, user_id: 4ff46818f83222daf9000001&gt; 1.9.3p194 :005 &gt; r.permissions.create(read: true, write: true, user_id: b.id) =&gt; #&lt;Permission _id: 4ff4684af83222daf9000005, _type: nil, read: true, write: true, user_id: 4ff4681bf83222daf9000002&gt; </code></pre> <p>Now I want to find all Resources for which User A has write access (which should be none)</p> <pre><code>1.9.3p194 :007 &gt; Resource.where('permissions.write' =&gt; true).where('permissions.user_id' =&gt; a.id).all.entries =&gt; [#&lt;Resource _id: 4ff46822f83222daf9000003, _type: nil&gt;] </code></pre> <p>This obviously fails because for each <code>where</code> clause there is a match. What would be the correct solution for this? Is there a way to do an <code>and</code> for embedded document queries?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload