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 8354097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:27:14+00:00 2026-06-09T09:27:14+00:00

What I want to do is group records in an Active Record relation by

  • 0

What I want to do is group records in an Active Record relation by a column on the :through model of a has_many :through relation, but I don’t know what the best way is to go about it.

First, my models:

User has_many :subscriptions
     has_many :courses, :through => :subscriptions

Subscription belongs_to "both"

Course has_many :subs...
       has_many :users, :through => :subscriptions

I grab the courses related to the current_user like so: @courses = current_user.courses
And I want to be able to do something like: @courses = current_user.courses.group('subscriptions.state') <– That doesn’t work obviously.

Also, after the records are grouped, what’s the proper way to loop through that in the view to display them? In another place in my app, I’ve used group_by and each_pair respectively to accomplish this, but that was with one individual model.

  • 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-09T09:27:16+00:00Added an answer on June 9, 2026 at 9:27 am
    current_user.courses.select('courses.*, subscriptions.state').group_by(&:state)
    

    This will return a hash and each key is a state. This works since the courses method is already doing a join.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to group RadioButtons, but I don't want to use the visible control
There is a handy dynamic attribute in active-record called find_or_create_by: Model.find_or_create_by_<attribute>(:<attribute> => ) But
I have two rails Active Record models groups and accounts group has_many :accounts id
I have run into something I don't know how to do with active record
I have a group model which has_many article model. And I want to use
I want to execute UPDATE command over a group of records, but it fails,
I want to group an sql statement by a column alias. In essense, I
I know there are already some solutions to this question but they don't seem
I want to group records in hour or day. The table A looks like:
I've done all of this in active record, but it's not going to work

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.