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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:38:22+00:00 2026-06-10T13:38:22+00:00

I have this class and these methods inside of it. class Purchase < ActiveRecord::Base

  • 0

I have this class and these methods inside of it.

class Purchase < ActiveRecord::Base
  belongs_to :user
  belongs_to :price
  belongs_to :unit_price

  scope :today, lambda { joins(:unit_price, :price).
                       where(:prices => { :date => Date.today }, 
                       :unit_prices =>  { :date => Date.today } ) }

  # not working                                 
  def total
    self.price.sum(:amount) + self.unit_price.sum(:amount)
  end
end

When I try to use my scope chain like this:

<%= current_user.purchases.today.map(&:total) %>

It gives me an empty Array result of []. I’m really trying to do this.

<%= number_to_currency(current_user.purchases.today.map(&:total)) %>

But this doesn’t work either for this error occurs.

undefined method `to_f' for []:Array

Why does it come back empty?

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-10T13:38:24+00:00Added an answer on June 10, 2026 at 1:38 pm

    Your today scope must be reducing the resultset to zero results. That is, if current_user has any purchases in the first place. Are you sure a Price‘s date is actually a Date? Are you sure the :prices association exists on Purchase (although I guess it would probably throw a NoMethodError there if it didn’t)?

    As for your undefined method to_f for []:Array, even if you weren’t working with an empty array, you’d still be trying to cast your array as a float. You’ll want to sum the array of prices you’ll get, and pass that to number_to_currency:

    number_to_currency(current_user.purchases.today.map(&:total).sum)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this as my create method inside my PostsController class PostsController < ApplicationController
I have a VB class inside which one of the methods accepts an array
I've been having this bothering recurring theme; let's just say, I have a class
I have this class: Class B { private String D; private String E; }
I have this class file call SMSHelper.cs First I just wanted to know is
I have this class in which I run a for loop 10 times. This
I have this class which i would like to map: public class Contract {
I have this class: class OriginalClass { string FirstItem; List<string> ListOfSecondItems; } I want
i have this class public class Image { public string url { get; set;
i have this class called MemoryManager, it is supposed to implement a simple smart

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.