Answers.Body: <p>What about creating an array of IDs with only those entries that do have an Thumbs image assigned to it and then check in your loop if you have to query the image at all:</p>
  
  <pre><code>{% set entries = craft.entries.section('myChannel') %}
  
  {# Get all potential "thumbs" using one! of the following ElementCriteriaModels #}
  {% set thumbs = craft.assets.sourceId(1) %}
  {% set thumbs = craft.assets.relatedTo(entries) %}
  
  {# Get the IDs of those entries that are related to a "thumbs" image #}
  {% set thumbEntryIds = craft.entries.section('myChannel').relatedTo({
      targetElement: thumbs,
      field: 'myImagesField',
  }).ids() %}
  
  {# Loop entries #}
  {% for entry in entries %}
  
      {# Only request image if relationship exists #}
      {% if entry.id in thumbEntryIds %}
          &lt;img src="{{ entry.myImagesField.first().url }}"/&gt;
      {% endif %}
  
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T10:43:00.100Z
Answers.Text: This isn't a bad idea. It feels "dirty" but it'll do the job, I suppose. Have you tested to see how many actual extra queries it'll add? Only 3 extra.. right?
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T10:47:44.857Z
Answers.Text: That's what I thought, but no, I didn't check how many queries there are and to be honest, I didn't test the code at all, @naboo.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T11:02:18.897Z
Answers.Text: One thing I'm totally not sure about, is how expensive that `relatedTo` parameter is to get the IDs of entries with thumbs. Maybe someone who knows more about what happens behind the code can help us to find that out?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T12:16:12.623Z
Answers.Text: When you run the `relatedTo` parameter on a regular Model it's not very "heavy" at all (in query execution time).
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T09:55:18.027Z
Answers.LastActivityDate: 2014-10-28T09:55:18.027Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>There isn’t currently, but it’s on our list to make it possible.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As of Craft 2.5, you can set relational field params to <code>'not :empty:'</code> or <code>':notempty:'</code> to only show elements where <em>something</em> is selected within the field; and you can set them to <code>':empty:'</code> to only show elements where <em>nothing</em> is selected within the field.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T10:01:35.297Z
Answers.Text: Brandon, that's not fair! "The List" answers will always be the most best-liked ones...
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T14:59:14.150Z
Answers.Text: @carlcs Just start answering everything with "It's on The List!"
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T09:30:14.770Z
Answers.LastActivityDate: 2015-12-05T15:18:59.727Z
Answers.LastEditDate: 2015-12-05T15:18:59.727Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>Is there any way to check if a relationship field has a value/is assigned to other elements (assets/entries) without having to add an extra query?</p>
  
  <p>I have a Channel with an Asset field assigned to it. Now let's say I have a list of 100 entries. This list should show a thumbnail and the entry title. I only want to "fetch" the related image thumbnail if the user has choosen to select any image to reduce the amount of queries used.</p>
  
  <p>The "standard" way which results in ~100 queries</p>
  
  <pre><code>{% set image = entry.image.first() %}
  {% if image %}
      &lt;img src="{{ image.url }}"/&gt;
  {% endif %}
  </code></pre>
  
  <p>The way I tried it, but <em>|length</em> adds (1) query making it ~100 queries + the queries for each found thumbnail</p>
  
  <pre><code>{% if entry.image|length &gt; 0 %}
      {% set image = entry.image.first() %}
      &lt;img src="{{ image.url }}"/&gt;
  {% endif %}
  </code></pre>
  
  <p>Is there a third option I can use to check (without a query) is the <em>entry.image</em> has any related content assigned to it?</p>
  
  <p>Thank you</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-27T16:06:44.097Z
Comments.Text: Didn't you ask that same question last week already? :D
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-27T16:38:32.657Z
Comments.Text: possible duplicate of [Optimize queries with list of image thumbnails](http://craftcms.stackexchange.com/questions/4166/optimize-queries-with-list-of-image-thumbnails)
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-27T20:36:39.080Z
Comments.Text: Hi @bradbell. Here I'm asking if there's any option to check if a relationship field has any relations without running a query. This to avoid running a query to check if (for example) an Asset field has any assets attached to it. My other thread was a general question about if there was any better practice for long list of entries with thumbnails. This is -slightly- different. :)
Comments.UserDisplayName: naboovalley
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-27T20:37:14.343Z
Comments.Text: @carlcs naa this is -slightly- different ;)
Comments.UserDisplayName: naboovalley
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-27T20:39:41.670Z
Comments.Text: To clarify (so I'm not missunderstod). I'm asking if there's any way to see if a relationship field has any relations directly on the field without running a query (like if there's any default value added to the *entry.field* data). I just used the "Asset thumbnail example" again.
Comments.UserDisplayName: naboovalley
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-28T09:57:40.470Z
Comments.Text: Ahh alright, I now see that little difference. I just undid my close vote ;)
Comments.UserDisplayName: carlcs
Tags: <templating>
Title: Check if a Relationship field has a relationship without extra query
ThreadText: # Check if a Relationship field has a relationship without extra query
  
  (Asked by: username_0 on 2014-10-27)
  
  Is there any way to check if a relationship field has a value/is assigned to other elements (assets/entries) without having to add an extra query?
  
  I have a Channel with an Asset field assigned to it. Now let's say I have a list of 100 entries. This list should show a thumbnail and the entry title. I only want to "fetch" the related image thumbnail if the user has choosen to select any image to reduce the amount of queries used.
  
  The "standard" way which results in ~100 queries
  
  ```
  {% set image = entry.image.first() %}
  {% if image %}
      <img src="{{ image.url }}"/>
  {% endif %}
  
  ```
  
  The way I tried it, but *|length* adds (1) query making it ~100 queries + the queries for each found thumbnail
  
  ```
  {% if entry.image|length > 0 %}
      {% set image = entry.image.first() %}
      <img src="{{ image.url }}"/>
  {% endif %}
  
  ```
  
  Is there a third option I can use to check (without a query) is the *entry.image* has any related content assigned to it?
  
  Thank you
  
  **Tags:** templating
  
  > **Comments:**
  > **username_1**: Didn't you ask that same question last week already? :D  
  > **username_2**: possible duplicate of [Optimize queries with list of image thumbnails](http://craftcms.stackexchange.com/questions/4166/optimize-queries-with-list-of-image-thumbnails)  
  > **username_0**: Hi @username_2. Here I'm asking if there's any option to check if a relationship field has any relations without running a query. This to avoid running a query to check if (for example) an Asset field has any assets attached to it. My other thread was a general question about if there was any better practice for long list of entries with thumbnails. This is -slightly- different. :)  
  > **username_0**: @username_1 naa this is -slightly- different ;)  
  > **username_0**: To clarify (so I'm not missunderstod). I'm asking if there's any way to see if a relationship field has any relations directly on the field without running a query (like if there's any default value added to the *entry.field* data). I just used the "Asset thumbnail example" again.  
  > **username_1**: Ahh alright, I now see that little difference. I just undid my close vote ;)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2014-10-28. Score: 7 | Accepted answer
  
  There isn’t currently, but it’s on our list to make it possible.
  
  **UPDATE**
  
  As of Craft 2.5, you can set relational field params to `'not :empty:'` or `':notempty:'` to only show elements where *something* is selected within the field; and you can set them to `':empty:'` to only show elements where *nothing* is selected within the field.
  
  > **Comments:**
  > **username_1**: username_3, that's not fair! "The List" answers will always be the most best-liked ones...  
  > **username_2**: @username_1 Just start answering everything with "It's on The List!"  
  
  ---
  
  ### Answer by username_1 on 2014-10-28. Score: 1
  
  What about creating an array of IDs with only those entries that do have an Thumbs image assigned to it and then check in your loop if you have to query the image at all:
  
  ```
  {% set entries = craft.entries.section('myChannel') %}
  
  {# Get all potential "thumbs" using one! of the following ElementCriteriaModels #}
  {% set thumbs = craft.assets.sourceId(1) %}
  {% set thumbs = craft.assets.relatedTo(entries) %}
  
  {# Get the IDs of those entries that are related to a "thumbs" image #}
  {% set thumbEntryIds = craft.entries.section('myChannel').relatedTo({
      targetElement: thumbs,
      field: 'myImagesField',
  }).ids() %}
  
  {# Loop entries #}
  {% for entry in entries %}
  
      {# Only request image if relationship exists #}
      {% if entry.id in thumbEntryIds %}
          <img src="{{ entry.myImagesField.first().url }}"/>
      {% endif %}
  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: This isn't a bad idea. It feels "dirty" but it'll do the job, I suppose. Have you tested to see how many actual extra queries it'll add? Only 3 extra.. right?  
  > **username_1**: That's what I thought, but no, I didn't check how many queries there are and to be honest, I didn't test the code at all, @naboo.  
  > **username_1**: One thing I'm totally not sure about, is how expensive that `relatedTo` parameter is to get the IDs of entries with thumbs. Maybe someone who knows more about what happens behind the code can help us to find that out?  
  > **username_0**: When you run the `relatedTo` parameter on a regular Model it's not very "heavy" at all (in query execution time).  
  
  ---
  
  