title: How can I define my association correctly?
body: <p>Why does this code return an error?<br>
  Is it because my association from <code>Community</code> or <code>CommunityTopics</code> are not set up correctly?</p>
  
  <blockquote>
    <p>undefined method `user_profile' for nil:NilClass</p>
  </blockquote>
  
  <pre><code>  &lt;% @community.community_topics.each do |topic| %&gt;
      &lt;li&gt;
      &lt;%= link_to topic.user.user_profile.nickname, community_topic_path(@community, topic) %&gt;
      &lt;/li&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>I have 4 models such as  </p>
  
  <ol>
  <li>User</li>
  <li>UserProfile (This is always set with User one to one)</li>
  <li>Community</li>
  <li><p>CommunityTopic</p>
  
  <ul>
  <li>Users can create Community as much as they want.</li>
  <li>Users can post Topics to any Community as much as they want.</li>
  </ul></li>
  </ol>
  
  <p>In this case how the association should be like?<br>
  Is my statement fine??</p>
  
  <p><strong>User</strong></p>
  
  <pre><code>has_one :user_profile
  has_many :communities
  has_many :community_topics
  </code></pre>
  
  <p><strong>UserProfile</strong></p>
  
  <pre><code>belongs_to :user
  </code></pre>
  
  <p><strong>Community</strong></p>
  
  <pre><code>belongs_to :user
  has_many :community_topics
  </code></pre>
  
  <p><strong>CommunityTopics</strong></p>
  
  <pre><code>belongs_to :user
  belongs_to :community
  </code></pre>