Answers.Body: <p>As noted in the comments above, eager-loading is not yet compatible with Live Preview. A clean workaround is to disable eager-loading in the template when the page is being viewed in Live Preview:</p>
  
  <pre><code>{% if craft.request.isLivePreview %}
      {% set images = entry.gallery %}
  {% else %}
      {% set images = craft.assets({
          relatedTo: {sourceElement: entry.id, field: 'gallery'}
      }) %}
  {% endif %}
  
  {% for image in images %}
      {{ image.url }}&lt;br /&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-13T17:55:18.137Z
Answers.LastActivityDate: 2017-06-13T17:55:18.137Z
Answers.UserDisplayName: Kevin Smith
Body: <p>I have an Entry Type with a field called Gallery with a handle of <code>gallery</code>. Here are two examples of how I can access the Gallery field in a template:</p>
  
  <p><code>{% set images = entry.gallery %}</code></p>
  
  <pre><code>{% set images = craft.assets({
      source:    'images',
      relatedTo: {sourceElement: entry.id, field: 'gallery'},
      order:     'sortOrder'
  }) %}
  </code></pre>
  
  <p><strong>The problem:</strong></p>
  
  <p>The first method works with Live Preview, the second does not. This wouldn't be an issue, but it's my understanding that we have to use the second method to eager load elements. So if I wanted to eager load a relationship on my gallery images, then Live Preview wouldn't work.</p>
  
  <p>Is anyone else running into this issue? Is it a known bug or a misunderstanding of the implementation?</p>
  
  <p>Thanks</p>
  
  <p><strong>Edit</strong></p>
  
  <p>To clarify via Brad's question, the following is the only thing in my template. This does NOT work with Live Preview:</p>
  
  <pre><code>{% set images = craft.assets({
      relatedTo: {sourceElement: entry.id, field: 'gallery'}
  }) %}
  {% for image in images %}
      {{ image.url }}&lt;br /&gt;
  {% endfor %}
  </code></pre>
  
  <p>The <code>entry</code> variable is populated defining the Entry Template in the section settings.</p>
  
  <p>Using the following WORKS with live preview:</p>
  
  <pre><code>{% set images = entry.gallery %}
  {% for image in images %}
      {{ image.url }}&lt;br /&gt;
  {% endfor %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-01T22:13:04.070Z
Comments.Text: Pretty sure it's a misunderstanding.  What's the code for where you initially set `entry` in the first example.
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-01T22:25:48.220Z
Comments.Text: Thanks Brad. Entry comes loaded with the template. It's the one Craft sets based on section settings.
Comments.UserDisplayName: Peter Tell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-01T22:41:46.383Z
Comments.Text: Updated my response with full examples Brad. There are no plugins installed and that code is the only thing in my template files. They both render the exact same thing on the front end after the entry is saved as expected.
Comments.UserDisplayName: Peter Tell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-04T20:34:43.100Z
Comments.Text: I tried to replicate this: Created a channel with one `gallery` field, created an entry, added two images to the field, and in the template fetched them both ways, and looped through the urls. In live preview, both lists of images initially show both urls. But when I add or remove or reorder images from the field, only the list set with `entry.gallery` gets updated in live preview.
  
  Confirming that I see a difference too.
Comments.UserDisplayName: Marion Newlevant
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-07T12:11:46.870Z
Comments.Text: I have this same issue with a matrix field that is eager loaded.
Comments.UserDisplayName: Tom De Smet
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-07T14:54:28.917Z
Comments.Text: Yeah, this seems to be a persistent bug. The bummer on this one is you either have to choose between performance or usability or write a bloated conditional in your templates which doesn't seem to be the ideal solution.
Comments.UserDisplayName: Peter Tell
Tags: <live-preview>
Title: .find() not working with live preview?
ThreadText: # .find() not working with live preview?
  
  (Asked by: username_0 on 2016-07-01)
  
  I have an Entry Type with a field called Gallery with a handle of `gallery`. Here are two examples of how I can access the Gallery field in a template:
  
  `{% set images = entry.gallery %}`
  
  ```
  {% set images = craft.assets({
      source:    'images',
      relatedTo: {sourceElement: entry.id, field: 'gallery'},
      order:     'sortOrder'
  }) %}
  
  ```
  
  **The problem:**
  
  The first method works with Live Preview, the second does not. This wouldn't be an issue, but it's my understanding that we have to use the second method to eager load elements. So if I wanted to eager load a relationship on my gallery images, then Live Preview wouldn't work.
  
  Is anyone else running into this issue? Is it a known bug or a misunderstanding of the implementation?
  
  Thanks
  
  **Edit**
  
  To clarify via username_1's question, the following is the only thing in my template. This does NOT work with Live Preview:
  
  ```
  {% set images = craft.assets({
      relatedTo: {sourceElement: entry.id, field: 'gallery'}
  }) %}
  {% for image in images %}
      {{ image.url }}<br />
  {% endfor %}
  
  ```
  
  The `entry` variable is populated defining the Entry Template in the section settings.
  
  Using the following WORKS with live preview:
  
  ```
  {% set images = entry.gallery %}
  {% for image in images %}
      {{ image.url }}<br />
  {% endfor %}
  
  ```
  
  **Tags:** live-preview
  
  > **Comments:**
  > **username_1**: Pretty sure it's a misunderstanding.  What's the code for where you initially set `entry` in the first example.  
  > **username_0**: Thanks username_1. Entry comes loaded with the template. It's the one Craft sets based on section settings.  
  > **username_0**: Updated my response with full examples username_1. There are no plugins installed and that code is the only thing in my template files. They both render the exact same thing on the front end after the entry is saved as expected.  
  > **username_2**: I tried to replicate this: Created a channel with one `gallery` field, created an entry, added two images to the field, and in the template fetched them both ways, and looped through the urls. In live preview, both lists of images initially show both urls. But when I add or remove or reorder images from the field, only the list set with `entry.gallery` gets updated in live preview.  Confirming that I see a difference too.  
  > **username_3**: I have this same issue with a matrix field that is eager loaded.  
  > **username_0**: Yeah, this seems to be a persistent bug. The bummer on this one is you either have to choose between performance or usability or write a bloated conditional in your templates which doesn't seem to be the ideal solution.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2017-06-13. Score: 2
  
  As noted in the comments above, eager-loading is not yet compatible with Live Preview. A clean workaround is to disable eager-loading in the template when the page is being viewed in Live Preview:
  
  ```
  {% if craft.request.isLivePreview %}
      {% set images = entry.gallery %}
  {% else %}
      {% set images = craft.assets({
          relatedTo: {sourceElement: entry.id, field: 'gallery'}
      }) %}
  {% endif %}
  
  {% for image in images %}
      {{ image.url }}<br />
  {% endfor %}
  
  ```
  
  
  ---
  
  