Answers.Body: <p>When you use the <code>craft.commerce.products</code> tag, it will return a criteria model, but you are treating it as the first result by accessing the title attribute.</p>
  
  <p>Try <code>{{ relatedProduct.first().title }}</code> instead.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-30T09:38:10.767Z
Answers.Text: You should always check if `first()` actually returns something before accessing properties or methods of the element model.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-30T15:57:52.243Z
Answers.Text: Thanks so much stephen & @carlcs. Looks like I heed to swat-up on the models
Answers.UserDisplayName: Britchie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-30T16:43:07.237Z
Answers.Text: Also, with the way I have the entries set-up presently, there will only ever be a single relationship returned. Is it possible to add the .first() to the main query instead of for every property? (couldn't find an example)
Answers.UserDisplayName: Britchie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-03T10:28:39.797Z
Answers.Text: @Britchie Exactly as you say, see here: http://craftcms.stackexchange.com/questions/289/how-do-i-target-the-first-item-in-a-twig-for-loop
Answers.UserDisplayName: Stephen Hamilton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-30T09:18:01.720Z
Answers.LastActivityDate: 2016-09-30T09:18:01.720Z
Answers.UserDisplayName: Stephen Hamilton
Body: <p>I'm working with Craft Commerce with a set of individual items in a product type and in another, I have packaged sets of the same. The sets (cardSets) have a relation field to select the required individual cards. The code is in the product entry template for the individual card and my intention is to highlight that the single card can be purchased in a set.</p>
  
  <p>I'm using the following code to do a simple reverse relationship but I can't get this to work and I'm not sure if I've got the syntax correct. I'm not seeing any return or error. How do I know if I'm getting an object back? </p>
  
  <pre><code>{% set relatedProduct = craft.commerce.products({
      type: 'cardSets',
      relatedTo: {
          targetElement: product,
          field: 'productsInSet'
          }
  }) %}
  
  {{ relatedProduct.title }}
  </code></pre>
  
Tags: <relations><plugin-craftcommerce>
Title: Reverse relationship in Craft Commerce?
ThreadText: # Reverse relationship in Craft Commerce?
  
  (Asked by: username_0 on 2016-09-30)
  
  I'm working with Craft Commerce with a set of individual items in a product type and in another, I have packaged sets of the same. The sets (cardSets) have a relation field to select the required individual cards. The code is in the product entry template for the individual card and my intention is to highlight that the single card can be purchased in a set.
  
  I'm using the following code to do a simple reverse relationship but I can't get this to work and I'm not sure if I've got the syntax correct. I'm not seeing any return or error. How do I know if I'm getting an object back?
  
  ```
  {% set relatedProduct = craft.commerce.products({
      type: 'cardSets',
      relatedTo: {
          targetElement: product,
          field: 'productsInSet'
          }
  }) %}
  
  {{ relatedProduct.title }}
  
  ```
  
  **Tags:** relations, plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-09-30. Score: 2 | Accepted answer
  
  When you use the `craft.commerce.products` tag, it will return a criteria model, but you are treating it as the first result by accessing the title attribute.
  
  Try `{{ relatedProduct.first().title }}` instead.
  
  > **Comments:**
  > **username_2**: You should always check if `first()` actually returns something before accessing properties or methods of the element model.  
  > **username_0**: Thanks so much username_1 & @username_2. Looks like I heed to swat-up on the models  
  > **username_0**: Also, with the way I have the entries set-up presently, there will only ever be a single relationship returned. Is it possible to add the .first() to the main query instead of for every property? (couldn't find an example)  
  > **username_1**: @username_0 Exactly as you say, see here: http://craftcms.stackexchange.com/questions/289/how-do-i-target-the-first-item-in-a-twig-for-loop  
  
  ---
  
  