<p>There could be corrupt data in your remote database. I tried to answer a similar case some weeks ago: <a href="https://craftcms.stackexchange.com/q/5164/125">"Entries related to Users"</a>, where the code was right, but Craft returned the wrong entries.</p>

<p>I'd check if it helps to clear the database and do a fresh import from you local environment.</p>

CC BY-SA 3.0
2015-03-04T11:55:45.857Z
Yep. You're right. That was it. Going to be a pain updating these entries again but at least I caught it early.
Dennis Eusebio
CC BY-SA 3.0
2015-03-04T14:02:19.573Z
Although, I did this and started to re-enter in the relations and it happened again :(
Dennis Eusebio
CC BY-SA 3.0
2015-03-04T14:06:00.937Z
Ohh no, @Dennis. I'm really sorry for you! :( ...best thing you can probably do now is to contact P&T support.
carlcs
CC BY-SA 3.0
2015-03-04T08:25:11.367Z
2015-03-04T08:25:11.367Z
2017-04-13T12:51:00.200Z
carlcs
<p><strong>So I have these sections and fields that I'm dealing with:</strong></p>

<p>candidates</p>

<ul>
<li>Name</li>
<li>Pic</li>
<li>Bio</li>
</ul>

<p>answers</p>

<ul>
<li>relatedQuestion</li>
<li>Body</li>
<li>relatedCandidate</li>
</ul>

<p>questions</p>

<ul>
<li>title</li>
<li>body</li>
</ul>

<p>candidateGroup</p>

<ul>
<li>title</li>
</ul>

<p><strong>I'm outputting a single page using the question:</strong></p>

<p>Question and Info</p>

<p>Candidate Group Name</p>

<ul>
<li><p>Candidate</p>

<ul>
<li>Answer</li>
</ul></li>
<li><p>Candidate</p>

<ul>
<li>Answer</li>
</ul></li>
</ul>

<p>... Repeats ...</p>

<p><strong>So my current setup looks like this:</strong></p>

<pre><code>{# Get the answers that are related to the question entry #}
{% set relatedAnswers = craft.entries.section('answers').relatedTo(entry) %}

{# Loop through categories #}
{% for category in craft.categories.group('candidateGroup') %}

  {{ category.title }}

  {# Get me the candidates that belong to this category and have an answer related to this question entry #}
  {% set candidateAnswers = craft.entries.section('candidates').relatedTo('and',
    category,
    relatedAnswers
  ) %}
  {% for candidateAnswer in candidateAnswers %}

    {# Output the candidate's info and their answer #}

  {% endfor %}
{% endfor %}
</code></pre>

<p>I feel like this is right and it seems to be working locally but when I get it live on the site it's having issues recognizing what "entry" it's a part of. No matter what entry I go to via url, it outputs the same answers.</p>

<p>I tried to do a simple loop to see what relatedAnswers outputs, and it's the right information. It's just getting lost in that inner loop somehow.</p>

<p><strong>Update:</strong>
I believe it is working locally just fine. But for some reason, on my Media Temple hosted site, it's not outputting in the same manner. I double and triple checked all the fields and couldn't find any differences?</p>

<p><strong>Update 2:</strong>
I can't figure this out for the life of me. It works fine on local, just not on production. I checked the database to see if there anything out of the ordinary and it looked fine to me. I installed the introvert plugin to see if the relations were working properly and they seem to be just fine. Kind of lost at this point.</p>

<p><strong>Updated 3</strong>
I tried it another way:</p>

<pre><code>&lt;div class="question__candidates"&gt;
  {% for category in craft.categories.group('candidateGroup') %}
    &lt;div class="question__row"&gt;
      &lt;h3&gt;{{ category.title }}&lt;/h3&gt;
      {% for answerEntry in craft.entries.section('answers').relatedTo('and',category,entry) %}
        &lt;div class="question__candidates__card"&gt;
          &lt;div class="question__candidates__card__header"&gt;
            &lt;div class="question__candidates__card__header__avatar"&gt;
              {% set relatedCandidate = craft.entries.section('candidates').relatedTo(answerEntry).first() %}
              {% set asset = relatedCandidate.avatar.first() %}
              &lt;a href="{{ relatedCandidate.url }}"&gt;&lt;img src="{{ asset.url }}" /&gt;&lt;/a&gt;
            &lt;/div&gt;
            &lt;div class="question__candidates__card__header__title"&gt;
              &lt;h4&gt;&lt;a href="{{ relatedCandidate.url }}"&gt;{{ relatedCandidate.title }}&lt;/a&gt;&lt;/h4&gt;
              &lt;p&gt;
                {% set categoryCandidate = relatedCandidate.group.group('candidateGroup').first() %}
                {{ categoryCandidate.title }}
              &lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="question__candidates__card__content"&gt;
            {{ answerEntry.body }}
          &lt;/div&gt;
        &lt;/div&gt;
      {% else %}
        &lt;div class="slate blank"&gt;
          There are no answers from candidates in this group yet.
        &lt;/div&gt;
      {% endfor %}
    &lt;/div&gt;
  {% endfor %}
&lt;/div&gt;
</code></pre>

<p>Unlike the other solution, my problem on the server is that it doesn't return any entries at all. I'm assuming it has something to do with that 'relatedTo(entry)'. It's like it doesn't recognize that at all. In the first case it just ignores it and  in this case it's like it can't read it.</p>

CC BY-SA 3.0
2015-03-05T20:03:35.213Z
I've responded to both of your support tickets... are you not getting them?
Brad Bell
<relations>
How to structure this multiple relation?
# How to structure this multiple relation?

(Asked by: username_0 on 2015-03-04)

So I have these sections and fields that I'm dealing with:
candidates

Name
Pic
Bio

answers

relatedQuestion
Body
relatedCandidate

questions

title
body

candidateGroup

title

I'm outputting a single page using the question:
Question and Info
Candidate Group Name

Candidate

Answer

Candidate

Answer


... Repeats ...
So my current setup looks like this:
{# Get the answers that are related to the question entry #}
{% set relatedAnswers = craft.entries.section('answers').relatedTo(entry) %}

{# Loop through categories #}
{% for category in craft.categories.group('candidateGroup') %}

  {{ category.title }}

  {# Get me the candidates that belong to this category and have an answer related to this question entry #}
  {% set candidateAnswers = craft.entries.section('candidates').relatedTo('and',
    category,
    relatedAnswers
  ) %}
  {% for candidateAnswer in candidateAnswers %}

    {# Output the candidate's info and their answer #}

  {% endfor %}
{% endfor %}

I feel like this is right and it seems to be working locally but when I get it live on the site it's having issues recognizing what "entry" it's a part of. No matter what entry I go to via url, it outputs the same answers.
I tried to do a simple loop to see what relatedAnswers outputs, and it's the right information. It's just getting lost in that inner loop somehow.
Update:
I believe it is working locally just fine. But for some reason, on my Media Temple hosted site, it's not outputting in the same manner. I double and triple checked all the fields and couldn't find any differences?
Update 2:
I can't figure this out for the life of me. It works fine on local, just not on production. I checked the database to see if there anything out of the ordinary and it looked fine to me. I installed the introvert plugin to see if the relations were working properly and they seem to be just fine. Kind of lost at this point.
Updated 3
I tried it another way:

  {% for category in craft.categories.group('candidateGroup') %}
    
      {{ category.title }}
      {% for answerEntry in craft.entries.section('answers').relatedTo('and',category,entry) %}
        
          
            
              {% set relatedCandidate = craft.entries.section('candidates').relatedTo(answerEntry).first() %}
              {% set asset = relatedCandidate.avatar.first() %}
              
            
            
              {{ relatedCandidate.title }}
              
                {% set categoryCandidate = relatedCandidate.group.group('candidateGroup').first() %}
                {{ categoryCandidate.title }}
              
            
          
          
            {{ answerEntry.body }}
          
        
      {% else %}
        
          There are no answers from candidates in this group yet.
        
      {% endfor %}
    
  {% endfor %}


Unlike the other solution, my problem on the server is that it doesn't return any entries at all. I'm assuming it has something to do with that 'relatedTo(entry)'. It's like it doesn't recognize that at all. In the first case it just ignores it and  in this case it's like it can't read it.

**Tags:** relations

> **Comments:**
> **username_1**: I've responded to both of your support tickets... are you not getting them?  

---

## 1 answers

### Answer by username_2 on 2015-03-04. Score: 1

There could be corrupt data in your remote database. I tried to answer a similar case some weeks ago: ["Entries related to Users"](https://craftcms.stackexchange.com/q/5164/125), where the code was right, but Craft returned the wrong entries.

I'd check if it helps to clear the database and do a fresh import from you local environment.

> **Comments:**
> **username_0**: Yep. You're right. That was it. Going to be a pain updating these entries again but at least I caught it early.  
> **username_0**: Although, I did this and started to re-enter in the relations and it happened again :(  
> **username_2**: Ohh no, @Dennis. I'm really sorry for you! :( ...best thing you can probably do now is to contact P&T support.  

---

