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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:05:47+00:00 2026-06-05T06:05:47+00:00

In a Rails app, I have a User model and a Posts model. I

  • 0

In a Rails app, I have a User model and a Posts model.

I have a :post_rate method

def self.post_rate(month_start, month_end)
     where('created_at >= ? AND <= ?', month_start, month_end).map { |p| p.created_at.beginning_of_day }.uniq.size.to_f / (month_start - month_end).to_f 
end

This shows the percentage of days a User posted in a given time frame.

On a User show page I want to display a summary of the user’s post frequency by month. I need an array that looks like:

['Month', 'Frequency'],
['Jan',  0.1],
['Feb',  0.5],
['Mar',  0.9],
etc..

(the labels and titles are needed).

How can I achieve this?

What performance considerations do I need to think about?

Thanks

  • 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-05T06:05:49+00:00Added an answer on June 5, 2026 at 6:05 am

    If your rails is recent enough, you can use ActiveRecord’s query smarts to compile a query that does this in SQL. You can pass a function to a GROUP BY clause via group:

    where('created_at >= ? AND <= ?', month_start, month_end).group("to_char(created_at,'MONTH')").count()
    

    The above works for PostgreSQL. You may have to change the arg to group to get it to work with a different database.

    The result will be in the form of a hash whose keys are the GROUP BY column and whose values are the counts. You will probably need to do some munging to get the data in the format you require.

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

Sidebar

Related Questions

I have a Rails app, with 2 models : a User model and a
I have a Rails 3.1 app with a User model and a Venue model.
I have a rails app that is using Devise, with a User model, no
I'm building an app in Rails 3. I currently have a user model which
I have a pretty basic 'User' model, control & views for a Rails app.
In my rails app I have a User model. In that model I have
I have a model User and Listing in my rails app. User has many
In a Rails app I have several integer attributes on a model. A user
In my Rails 3 app, I have a model called User with id and
I have a User model in my rails app as follows class User <

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.