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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:32:23+00:00 2026-05-28T17:32:23+00:00

I have the following model: activity_types: id, name activities: id, id_activity_type, occurrences, date (other

  • 0

I have the following model:

activity_types: id, name

activities: id, id_activity_type, occurrences, date (other fields)

The activities table store how many times an activity occurs by day. But now I want to show to the user how many activities from each type occurred by month.

I got the following solution based on this post which seems ok:

Activity.all(:joins => :activity_types,
             :select => "activity_types.id, activity_types.name, SUM(activities.occurrences) as occurrences",
             :group => "activity_types.id, activity_types.name",
             :order => "activity_types.id")

but this seems a lot of code for the rails standards and rails API says it’s deprecated.

I found the following solution which is a lot simple:

Activity.sum(:occurrences).group(:activity_type_id)

Which returns an hash with activity_type_id => occurrences.

What shall I do to get the following hash: activity_type.name => occurrences ?

  • 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-28T17:32:24+00:00Added an answer on May 28, 2026 at 5:32 pm

    If the original query worked, then just try rewriting it with Rails 3 syntax:

    Activity.joins(:activity_types)
      .select("activity_types.id, activity_types.name, SUM(activities.occurrences) as occurrences")
      .group("activity_types.id, activity_types.name")
      .order("activity_types.id")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data model: Group(name, color, activities*) // one group may have
I have the following model: public class Contact { public Contact() { Name =
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following model: class User: name = models.CharField( max_length = 200 )
I have the following model: public class Tag { public int Id { get;
I have the following model: WebPromocion: connection: doctrine tableName: WebPromocion columns: id: type: integer(4)
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
I have the following model association: a student model and has_many scores. I need
I have the following model setup - a User is interested in projects in

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.