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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:31:32+00:00 2026-06-08T17:31:32+00:00

I have a has_many (through accounts) association between the model User and the model

  • 0

I have a has_many (through accounts) association between the model User and the model Player.

I would like to know what is the best way to query all the users, and for each returned record get the associated players usernames attributes (in comma separated values).

So, if for example, a User named ‘John’ is associated with 3 players with usernames ‘john_black’, ‘john_white’, ‘john_yellow’. I would like the query to return not just the User attributes but also an attribute called, for example, player_username, that would have this value: john_black, john_white, john_yellow.

I have tried the following Arel query in the User model:

select(`users`.*).select("GROUP_CONCAT(`players`.`username` SEPARATOR ',') AS comma_username").joins(:user)
      .joins(:accounts => :player )

Which gives me the following SQL:

SELECT `users`.*, GROUP_CONCAT(`players`.`username` SEPARATOR ',') AS comma_username FROM `users` INNER JOIN `accounts` ON `accounts`.`user_id` = `users`.`id` INNER JOIN `players` ON `players`.`id` = `accounts`.`player_id` 

If I execute this in MySQL console it works, but if I try to fetch from the model, I don’t get the comma separated values.

What am I missing?

  • 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-08T17:31:36+00:00Added an answer on June 8, 2026 at 5:31 pm

    I believe ActiveRecord maps all columns retrieved by the SQL query with all attributes you have on your model, which in most of the cases are exactly the same. Maybe if you create a virtual accessor on your model, ActiveRecord could map your virtual column to the virtual attribute.

    class User
    
      attr_accessible :player_username
      ...
    

    Give it a try and see if this works.

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

Sidebar

Related Questions

I have Product model and it has many categories with a has_many :through association
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
I have a User has_many :accounts, through: :roles and a User has_many :owned_accounts, through:
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have an Organization model that has_many users through affiliations. And, in the form
I have Accounts, Tickets, Groups: # group.rb has_many :tickets has_many :accounts, :through => :assignments
I have an Account model and a User model: class Account < ActiveRecord::Base has_many
I have a has_many :through form where I can't get an extra attribute to
Is it possible to have multiple has_many :through relationships that pass through each other
I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events

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.