Displaying images from a matrix field from a related field I have a section that is a single type. On this page there is a relation ship ('entries') field where the user can select a related photo gallery. Photo gallery is its own section with a matrix field on it that contains image files and image names. How when I am displaying the single page pull in the matrix field to display an image gallery. This is what I have tried but not with much luck.

I have tried 

  {% if entry.associatedGallery is not null %}
    {% set theGallery = craft.entries.section('bb').relatedTo(entry.associatedGallery) %}
      &lt;div class="galleria associated-gallery"&gt;
        {% for matrixBlock in theGallery.albumImages %}
          {% set asset = matrixBlock.imageFile.first() %}
            {% if asset %}
              &lt;a href="{{ asset.getUrl() }}"&gt;&lt;img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"&gt;&lt;/a&gt;
            {% endif %}
        {% endfor %}
      &lt;/div&gt;
    {% else %}
      &lt;h1&gt; Apparently its null&lt;/h1&gt;  
  {% endif %}


and 

  {% if entry.associatedGallery is not null %}
    {% set theGallery = entry.associatedGallery %}
      &lt;div class="galleria associated-gallery"&gt;
        {% for matrixBlock in theGallery.albumImages %}
          {% set asset = matrixBlock.imageFile.first() %}
            {% if asset %}
              &lt;a href="{{ asset.getUrl() }}"&gt;&lt;img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"&gt;&lt;/a&gt;
            {% endif %}
        {% endfor %}
      &lt;/div&gt;
    {% else %}
      &lt;h1&gt; Apparently its null&lt;/h1&gt;  
  {% endif %}


I think I am missing something fundamental about how relationships are defined.
Relationship field is defined like:

{% for relatedItem in entry.relatedItemField %}
  {{ relatedItem.someField }} {{ relatedItem.someOtherField }}    
{% endfor %}


So maybe:

{% if entry.associatedGallery | length %}
    {% for theGallery in entry.associatedGallery %}
        {% for matrixBlock in theGallery.albumImages %}
            {% if matrixBlock.imageFile | length %}
                {% for asset in matrixBlock.imageFile.first() %}
                    &lt;a href="{{ asset.getUrl() }}"&gt;&lt;img src="{{ asset.getUrl('homeThumb') }}" data-title="{{ matrixBlock.imageName }}"&gt;&lt;/a&gt;
                {% endfor %}
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endif %}