Answers.Body: <p>Yes you're correct, <code>one()</code> will get the first matching element. However, in your code you're not asking for the first image block, but rather the first image from each image block (because each image field can have multiple images attached to it).</p>
  
  <p>The structure is basically something like this:</p>
  
  <pre><code>entry          &lt;- 1. The entry
    mainPost     &lt;- 2. The matrix field
      postImage  &lt;- 3. The matrix field's block
        image    &lt;- 4. The field inside the matrix field's block
  </code></pre>
  
  <p>We want to get the first result from #3 but you're currently only asking for the first result from #4. (It is probably a good idea to also ask for the first result from #4 as well btw.)</p>
  
  <p>I think something like the following would work in your case:</p>
  
  <pre><code>{% for block in entry.mainPost.type("postImage").limit(1).all() %}
    {% set image = block.image.one() %}
    {% if image %}
      &lt;img src="{{ image.getUrl('summaryImage')}}" alt=""&gt;
    {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>UPDATE: looks like you actually need to use <code>limit()</code> instead of <code>one()</code> when querying for the block.</p>
  
  <p>UPDATE 2: tacked on the <code>all()</code> to fix the deprecation!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-11T22:53:33.227Z
Answers.Text: @Robin thanks for the clarification. Lesson for us both!
Answers.UserDisplayName: Shad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T00:36:05.007Z
Answers.Text: Thanks for the response! I added your code in place of what I had and received the following error message: Impossible to access an attribute ("image") on a string variable ("46").
  
  If I remove .one() from entry.mainPost.type("postImage), my page loads, but without any images...
Answers.UserDisplayName: Shad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T00:38:06.940Z
Answers.Text: The line number it gives as having the problem is 
  
  {% set image = block.image.one() %}
Answers.UserDisplayName: Shad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T00:45:11.310Z
Answers.Text: Oops lol - I'll blame it on how late it is. I updated my post - looks like you'll have to use `limit(1)` vs `one()`. I tested it locally on one of my sites and this seems to actually work. :P
Answers.UserDisplayName: Jalen Davenport
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T00:49:37.893Z
Answers.Text: Thanks again! And yes, that's working as expected now. However, as I'm using Craft 3, I'm getting the Deprecation Error "Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them."
Answers.UserDisplayName: Shad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T00:55:42.087Z
Answers.Text: Ah yes, so just tack on `.all()` to the end and you should be good to go!
Answers.UserDisplayName: Jalen Davenport
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T01:08:16.497Z
Answers.Text: Fantastic! Thanks so much for your help Jalen!
Answers.UserDisplayName: Shad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T01:09:58.257Z
Answers.Text: No worries. Thanks for bearing with all my edits and suggestions...
Answers.UserDisplayName: Jalen Davenport
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T05:05:53.743Z
Answers.Text: @jalen `limit(1).all()` and, `one()` is actually the same. The first just returns an array with 1 element, the last returns the element directly. There is no need to loop through one element. You could remove the loop. If all fields are required you could do `entry.mainPost.type("postImage").one().image.one().getUrl()`
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-11T00:57:27.080Z
Answers.Text: @RobinSchambach, I tried adding what you suggested, and while I'm not getting an error, neither is the image showing now...
Answers.UserDisplayName: Shad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-11T05:03:10.490Z
Answers.Text: @Shad that wasn't a suggestion for you but a hint for Jalen how `one` and `all` works
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-10T00:13:12.957Z
Answers.LastActivityDate: 2018-04-10T00:55:24.440Z
Answers.LastEditDate: 2018-04-10T00:55:24.440Z
Answers.UserDisplayName: Jalen Davenport
Body: <p>I'm sure I'm doing something wrong here, but I'm at a loss as to what. I'm using Craft 3. My travel entry has 7 fields, one of those being a matrix field. My matrix has two blocks, <code>postText</code> and <code>postImage</code>.</p>
  
  <p>Here's the bit of code I'm working with:</p>
  
  <pre><code>{% for block in entry.mainPost.all() %}
    {% if block.type == "postImage" %}
      {% set image = block.image.one() %}
          {% if image %}
              &lt;img src="{{ image.getUrl('summaryImage')}}" alt=""&gt;
          {% endif %}
    {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>I have an entry that has two images in the Matrix field. When I load the page, both images load. The documentation states that using <code>.one()</code> <em>"Returns the first matching element"</em>. I have to assume that I'm not understanding the ElementCriteriaModel or Assets or the way Matrix works or something.</p>
  
Tags: <assets><matrix><image>
Title: block.image.one() shows more than one image
ThreadText: # block.image.one() shows more than one image
  
  (Asked by: username_0 on 2018-04-09)
  
  I'm sure I'm doing something wrong here, but I'm at a loss as to what. I'm using Craft 3. My travel entry has 7 fields, one of those being a matrix field. My matrix has two blocks, `postText` and `postImage`.
  
  Here's the bit of code I'm working with:
  
  ```
  {% for block in entry.mainPost.all() %}
    {% if block.type == "postImage" %}
      {% set image = block.image.one() %}
          {% if image %}
              <img src="{{ image.getUrl('summaryImage')}}" alt="">
          {% endif %}
    {% endif %}
  {% endfor %}
  
  ```
  
  I have an entry that has two images in the Matrix field. When I load the page, both images load. The documentation states that using `.one()` *"Returns the first matching element"*. I have to assume that I'm not understanding the ElementCriteriaModel or Assets or the way Matrix works or something.
  
  **Tags:** assets, matrix, image
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-10. Score: 3 | Accepted answer
  
  Yes you're correct, `one()` will get the first matching element. However, in your code you're not asking for the first image block, but rather the first image from each image block (because each image field can have multiple images attached to it).
  
  The structure is basically something like this:
  
  ```
  entry          <- 1. The entry
    mainPost     <- 2. The matrix field
      postImage  <- 3. The matrix field's block
        image    <- 4. The field inside the matrix field's block
  
  ```
  
  We want to get the first result from #3 but you're currently only asking for the first result from #4. (It is probably a good idea to also ask for the first result from #4 as well btw.)
  
  I think something like the following would work in your case:
  
  ```
  {% for block in entry.mainPost.type("postImage").limit(1).all() %}
    {% set image = block.image.one() %}
    {% if image %}
      <img src="{{ image.getUrl('summaryImage')}}" alt="">
    {% endif %}
  {% endfor %}
  
  ```
  
  UPDATE: looks like you actually need to use `limit()` instead of `one()` when querying for the block.
  
  UPDATE 2: tacked on the `all()` to fix the deprecation!
  
  > **Comments:**
  > **username_0**: Thanks for the response! I added your code in place of what I had and received the following error message: Impossible to access an attribute ("image") on a string variable ("46").  If I remove .one() from entry.mainPost.type("postImage), my page loads, but without any images...  
  > **username_0**: The line number it gives as having the problem is   {% set image = block.image.one() %}  
  > **username_1**: Oops lol - I'll blame it on how late it is. I updated my post - looks like you'll have to use `limit(1)` vs `one()`. I tested it locally on one of my sites and this seems to actually work. :P  
  > **username_0**: Thanks again! And yes, that's working as expected now. However, as I'm using Craft 3, I'm getting the Deprecation Error "Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them."  
  > **username_1**: Ah yes, so just tack on `.all()` to the end and you should be good to go!  
  > **username_0**: Fantastic! Thanks so much for your help username_1!  
  > **username_1**: No worries. Thanks for bearing with all my edits and suggestions...  
  > **username_2**: @username_1 `limit(1).all()` and, `one()` is actually the same. The first just returns an array with 1 element, the last returns the element directly. There is no need to loop through one element. You could remove the loop. If all fields are required you could do `entry.mainPost.type("postImage").one().image.one().getUrl()`  
  > **username_0**: @username_2, I tried adding what you suggested, and while I'm not getting an error, neither is the image showing now...  
  > **username_2**: @username_0 that wasn't a suggestion for you but a hint for username_1 how `one` and `all` works  
  > **username_0**: @username_2 thanks for the clarification. Lesson for us both!  
  
  ---
  
  