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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:45:36+00:00 2026-06-11T14:45:36+00:00

How can I iterate through an array of Activerecord::Relation objects? For instance, let’s say

  • 0

How can I iterate through an array of Activerecord::Relation objects? For instance, let’s say I have a Comment class and a User class and I’d like to get all the comment contents from 3 specific users (assuming comments belong to users and user_id is the foreign key):

>> @males = Comment.where('user_id IN (?)', ["123","456","789"])
=> [...] #Array of comment Activerecord::Relation objects

Now I’d like to iterate through comments_from_males and collect all the content attribute contents for each comment in the array.

To clarify, the following works but only for the first male returned, but I need all the comments for all males:

>> @males.first.comments.map(&:content)
=> ["first comment", "second comment"]
  • 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-11T14:45:38+00:00Added an answer on June 11, 2026 at 2:45 pm
    comments = @males.map {|user| user.comments.map(&:content)}.flatten
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of questions that the user will iterate through, they can
I am trying to iterate through array of objects in PHP, but I can't
How can one iterate through an array to manipulate or access different objects and
I Have to iterate through an array, such that I can get all values
I have this code to iterate through an array of objects: for (vehicleIndex in
Generally , how can you determine how to iterate through an array based on
I have a array of string say: String[] Fields=new String[]{RowField,RowField1} In which I can
If I have a type ArrayList<HashMap<String,String>> keys = functionWhichReturnsThisType(); How can I iterate through
Is there a method in which I can iterate through all objects that exist
With the following code I can iterate through the elements of the returned array:

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.