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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:14:35+00:00 2026-06-03T06:14:35+00:00

With some help, I was able to take a method that displays a word

  • 0

With some help, I was able to take a method that displays a word count for each day in a project and reduce it down to pretty much one database quere. Here’s the method

def project_range(project, start, finish, &blk)
  words = project.words.where(:wrote_on => start..finish)
  word_map = words.index_by(&:wrote_on)
  for day in start..finish
    word_count = word_map[day] ? word_map[day].quantity : 0
    blk.call(day, word_count)
  end
end

The original question is here:

Reducing database hits in Rails

Now though, we want to change the way users enter word counts for each day, however — we want to give them a chance to enter multiple counts for a day, instead of just one per day. This method, however, only returns the word count for the last day entered, not all the word counts for that specific day.

I tried changing index_by to group_by, but I got an undefined method error for quantity. So, my question is… how can I handle multiple entries for the same day, where there are two wrote_on objects for say, 5/4/2012?

  • 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-03T06:14:36+00:00Added an answer on June 3, 2026 at 6:14 am

    group_by worked with another modification. Changes are commented out.

    def project_range(project, start, finish, &blk)
        words = project.words.where(:wrote_on => start..finish)
        #word_map = words.index_by(&:wrote_on)
        word_map = words.group_by(&:wrote_on)
        for day in start..finish
          #word_count = word_map[day] ? word_map[day].quantity : 0
          word_count = word_map[day] ? word_map[day].sum(&:quantity) : 0
          blk.call(day, word_count)
        end
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help assigning a mouseover event to display some icons that start out
Need some help with DataFormatString in GridView. I have a Double value that needs
I'm looking for some help on how to take an attribute and process it
I need some help with a formatting function. I need to be able to
I have a method that will provide an array of model object. Some of
I could use some help with a RIA services query that I have been
I need some help on how to create a Navigation Controller that loads its
I have writing a method which needs to be able to take an arbitrary
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview

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.