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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:53:46+00:00 2026-05-31T13:53:46+00:00

I have a task model related to activities and i want to get a

  • 0

I have a task model related to activities and i want to get a array/hash with all Tasks and their related activities like this:

<% task_group =[
     ['Task1', {'Activity1' => 1, 'Activity2' => 2}], 
     ['Task2', {'Activity3' => 3, 'Activity4'=> 4}]
    ] %>

I came up with Task.includes(:activities).all.map(&:name) but this only gives me a list with activities and their name.

How can i get a complete list including nested relations?

My model defintion:

class Task < ActiveRecord::Base
  has_many :activities

class Activity < ActiveRecord::Base
  belongs_to :task
  • 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-31T13:53:47+00:00Added an answer on May 31, 2026 at 1:53 pm

    you can use:

    Task.includes(:activities).all.map{|t| [t.name, t.activities.inject({}){|H, a| H[a.name] = a.id; H}]}
    

    EDIT:

    Task.includes(:activities).all.map{|t| [t.name, t.activities.inject({}){|h, a| h[a.name] = a.id; h}]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model like this class Task(models.Model): name = models.CharField(max_length=100) ... class TaskForm(forms.ModelForm):
I have a model like this: class Task(models.model): TASK_STATUS_CHOICES = ( (uP, u'Pending'), (uA,
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
I have a Task model where every task has deadline. class Tasks(models.Model): assigned_to =
I have the following model: class Task(Model): solvers = ManyToManyField(User, related_name='slvrs') Now I want
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
Rails App. I have a Project and a Task Model. Tasks have a column
[Updated a bit] I have a Task model that has a :completed boolean attribute.
I have an interesting scenario. I have a task model which has a task
I have a model Task which will HABTM many TaskTargets. When it comes to

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.