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

  • SEARCH
  • Home
  • 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 4065260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:57:38+00:00 2026-05-20T15:57:38+00:00

Models: class User < ActiveRecord::Base has_many :attendances has_many :courses, :through => :attendances end class

  • 0

Models:

class User < ActiveRecord::Base
  has_many :attendances
  has_many :courses, :through => :attendances
end

class Course < ActiveRecord::Base
  has_many :attendances
end

class Attendance < ActiveRecord::Base
  belongs_to :user
  belongs_to :course
end

Migrations:

create_table(:users) do |t|
  t.string :name
end

create_table(:courses) do |t|
  t.string :name
end

create_table(:attendances) do |t|
  t.references :user, :course
  t.date :date
end

Question

I would like to query:

  • a list of all courses
  • the date of a given user’s last attendance (if any) for each course

What is the best way to tie the following together into a single query?

@courses = Course.all
@user = User.first
@attendances = @user.attendances.group('course_id').order('date DESC')

Note that there is a requirement to include courses that a user has not yet attended.

Any advice much appreciated.

Update

The result I am looking for is as follows:

Course            Last attended
===============================
Some course       2011-03-09
More training     Not Attended
Another course    2010-12-25

In SQL, I would write this query as:

SELECT * FROM courses AS c
LEFT OUTER JOIN attendances AS a ON a.course_id=c.id
WHERE a.user_id=1
GROUP BY a.course_id
ORDER BY a.date DESC;

I could always execute this SQL from within Rails, but I would prefer to avoid this and do things “the Rails way” if possible.

  • 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-05-20T15:57:39+00:00Added an answer on May 20, 2026 at 3:57 pm

    The “Rails-Way” is to define small finders in the model and then to chain them in the controllers.

    In class Attendance you could define a new method

    def self.last_attendance
      maximum("date")
    end
    

    In class Corse …

    def self.ordered
      order("name DESC")
    end
    

    And so on. In the controller then, you use them in different combinations. The big advantage of this approach is

    • Reusability of finder methods
    • Decoupling of classes
    • Better readability of code
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation # Models class User < ActiveRecord::Base has_many :items end class Items < ActiveRecord::Base
got 2 models: class User < ActiveRecord::Base has_many :posts end and class Post <
I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have models: class User < ActiveRecord::Base has_many :user_skills end class UserSkill < ActiveRecord::Base
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
Say I have these models class Project < ActiveRecord::Base has_many :comments end class Comment
I've got two models: class Solution < ActiveRecord::Base belongs_to :owner, :class_name => User, :foreign_key
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end

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.