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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:48:43+00:00 2026-06-16T18:48:43+00:00

I have a date (type:DateTime) column and a price (type:Decimal) column within my db.

  • 0

I have a date(type:DateTime) column and a price(type:Decimal) column within my db. Records are created and saved hourly. I need to create a rake task to go through all of my stored data and create an average price for each day.

Using ActiveRecord, how can I query all of my records, group together all records within each day, create a new average price from those 24 records, and save it as a new record in a different table?

I imagine it would be something like this psuedocode:

Sales.all
  loop through them and group records by day
    loop through each grouping and create average
      average = DailyAverage.create

FYI I have about 10,000 records.

  • 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-16T18:48:44+00:00Added an answer on June 16, 2026 at 6:48 pm

    Scope is nice way to select some records and group them. In Sales class define something like scope :by_date, ->(needful_date) { where{ date == needful_date} }. Then in another model(which relates to another table) define your calculations:

    def calculate_average_prices
      first_sale_date = # find your first sale date
      date_range = first_sale_date..Date.today
      grouped_sales = date_range.map{|date| Sales.by_date(date)}
      average_prices = grouped_sales.map do |sales|
        sales.map{|sale| sale.price.to_f}.average
      end
      # save your average prices
    end
    

    It’s just an idea of solution. You’d better understand it and implement yourself.

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

Sidebar

Related Questions

In mysql database i have this column called: Name: Date Type: datetime I have
I have a MySQL database , with a column that is date type DATETIME
I have an Oracle Date type to which I need to insert the current
I have a books model with a date type column named publish_date. On my
I have a column of type date, and another of type time, and I
I have a table with a field of type date within a MySQL database.
I have a table with a column ReceivedOn of type DateTime . I am
I have a column in my datatable which is of DateTime Type. I only
I have a column of type datetime with values like 2009-10-20 10:00:00 I would
I have a table of 5000 records with a date column. How do I

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.