Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:04:31+00:00 2026-06-13T11:04:31+00:00

I have a User , List , and Task models. A task belongs_to user

  • 0

I have a User, List, and Task models.

A task belongs_to user and list, a list has_many tasks and belongs_to user, and a user has_many list and has_many tasks.

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.

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.

In the list index view I want the first task created to display only if it is associated with the list.

Task model:

class Task < ActiveRecord::Base
  attr_accessible :completed, :description, :list_id, :user_id

  belongs_to :list
  belongs_to :user

  scope :completed, where(:completed => true)
  scope :incomplete, where(:completed => false)

  def self.most_recent
    first(:order => 'id ASC' ) # or whatever query you need to get the most recent
  end

end

List Index View-

<div class="span12 offset2"> 
 <% @lists.each do |list| %> 
  <div class="well row span4">
    <div class="span3">
    </div>  
    <div class="span4">   
      <span class="lead">
        <%= link_to "#{list.name}", list_url(list) %>
      </span>
      <p>
        <%= list.description %>
      </p><hr>
      <div class="pull-right" id="gage-<%= list.id %>" style="width:170px; height:100px;"></div>
      <br>  
      <span class="offset1">
        <% if Task.most_recent %>
          <span class="pull-left"> <%= image_tag Task.most_recent.user.gravatar_url, :class => "gravatar_index" %></span>
        <% end %>  
      </span>
    </div>
  <% end %>
</div>

I’m still new to rails but I know I’m very close. What am I overlooking here? emphasized text

===========

EDIT

I’ve tested @Baldrick’s solution and now seem to be getting another error

My view with described solution-

<% if Task.most_recent(list) %>
      <span class="pull-left"> <%= image_tag Task.most_recent.user.gravatar_url, :class => "gravatar_index" %></span>
<% end %>

I am now getting the error-

SQLite3::SQLException: no such column: tasks.list: SELECT  "tasks".* FROM "tasks"  WHERE "tasks"."list" = 4 ORDER BY id ASC LIMIT 1

I must not be understanding correctly because I have such a created column when I created the Task model

class CreateTasks < ActiveRecord::Migration
  def change
    create_table :tasks do |t|
      t.string :description
      t.boolean :completed, :default => false
      t.integer :list_id

      t.timestamps
    end
  end
end

Is this a case where I need to add an index to be able to make my query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:04:31+00:00Added an answer on June 13, 2026 at 11:04 am

    The method most_recent gives you the last create task of all tasks. What you wnat is the most recent task of a given list.

    You should give a parameter to the list

    def self.most_recent(list)
      where(list_id: list.id).order('id ASC').first # or whatever query you need to get the most recent
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method I wrote as: private void GetReceivedInvoiceTasks(User user, List<Task> tasks) {
i have list of rows that user select and i want to delete them,
I have a view that displays a list of models. Some of the properties
I have 3 models/tables: User, Task, and TaskInstance. The Task table is like a
I have 2 controllers, task and user. I need to create a view for
I'm using RABL to render JSON. I have these models: Project Task User The
I have a list with my employers project task list. When a user adds
In my app, I have a task list (no, it's not just another todo
I have a user asking me to list the dimensions and dimension attributes of
I have a class Bar that has a user-defined list of config keys and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.