title: Displaying associated records in rails
body: <p>I have a <code>User</code>, <code>List</code>, and <code>Task</code> models.</p>
  
  <p>A <strong>task</strong> <code>belongs_to</code> <em>user</em> and <em>list</em>, a <em>list</em> <code>has_many</code> <em>tasks</em> and <code>belongs_to</code> <em>user</em>, and a <em>user</em> <code>has_many</code> <em>list</em> and <code>has_many</code> <em>tasks</em>. </p>
  
  <p>I have part of what I want to display working in my index view, with just one snafu that I am getting hung up on.</p>
  
  <p>I'm able to display the first created task in the index view of all the lists. The problem is, the first task created is displayed for all of the lists, even if the list is NOT associated with the task. </p>
  
  <p>In the list index view I want the first task created to display only if it is associated with the list. </p>
  
  <p>Task model:
  </p>
  
  <pre><code>class Task &lt; ActiveRecord::Base
    attr_accessible :completed, :description, :list_id, :user_id
  
    belongs_to :list
    belongs_to :user
  
    scope :completed, where(:completed =&gt; true)
    scope :incomplete, where(:completed =&gt; false)
  
    def self.most_recent
      first(:order =&gt; 'id ASC' ) # or whatever query you need to get the most recent
    end
  
  end
  </code></pre>
  
  <p>List Index View-</p>
  
  <pre><code>&lt;div class="span12 offset2"&gt; 
   &lt;% @lists.each do |list| %&gt; 
    &lt;div class="well row span4"&gt;
      &lt;div class="span3"&gt;
      &lt;/div&gt;  
      &lt;div class="span4"&gt;   
        &lt;span class="lead"&gt;
          &lt;%= link_to "#{list.name}", list_url(list) %&gt;
        &lt;/span&gt;
        &lt;p&gt;
          &lt;%= list.description %&gt;
        &lt;/p&gt;&lt;hr&gt;
        &lt;div class="pull-right" id="gage-&lt;%= list.id %&gt;" style="width:170px; height:100px;"&gt;&lt;/div&gt;
        &lt;br&gt;  
        &lt;span class="offset1"&gt;
          &lt;% if Task.most_recent %&gt;
            &lt;span class="pull-left"&gt; &lt;%= image_tag Task.most_recent.user.gravatar_url, :class =&gt; "gravatar_index" %&gt;&lt;/span&gt;
          &lt;% end %&gt;  
        &lt;/span&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I'm still new to rails but I know I'm very close. What am I overlooking here? <em>emphasized text</em></p>
  
  <p>===========</p>
  
  <h1><strong>EDIT</strong></h1>
  
  <p>I've tested @Baldrick's solution and now seem to be getting another error</p>
  
  <p>My view with described solution-</p>
  
  <pre><code>&lt;% if Task.most_recent(list) %&gt;
        &lt;span class="pull-left"&gt; &lt;%= image_tag Task.most_recent.user.gravatar_url, :class =&gt; "gravatar_index" %&gt;&lt;/span&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I am now getting the error-  </p>
  
  <pre><code>SQLite3::SQLException: no such column: tasks.list: SELECT  "tasks".* FROM "tasks"  WHERE "tasks"."list" = 4 ORDER BY id ASC LIMIT 1
  </code></pre>
  
  <p>I must not be understanding correctly because I have such a created column when I created the Task model</p>
  
  <pre><code>class CreateTasks &lt; ActiveRecord::Migration
    def change
      create_table :tasks do |t|
        t.string :description
        t.boolean :completed, :default =&gt; false
        t.integer :list_id
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>Is this a case where I need to add an index to be able to make my query?</p>