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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:05:37+00:00 2026-05-16T12:05:37+00:00

Not sure on my Ruby syntax here. I want to define a method that

  • 0

Not sure on my Ruby syntax here.

I want to define a method that I can call like this: client.invoices.average_turnaround. So my average_turnaround method needs to work with a collection of ActiveRecord objects.

Here’s my code thus far:

class Invoice < ActiveRecord::Base
  ...
  def self.average_turnaround
    return self.map(&:turnaround).inject(:+) / self.count
  end
end

So I’m trying to find the sum of the turnaround times for each invoice, then divide it by the total number of invoices.

Ruby is complaining that there is no map method defined for Class. I was expecting self to be an Array.

How do I write a method that works on a collection of Invoices and uses the map function? Where am I going wrong?

  • 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-16T12:05:37+00:00Added an answer on May 16, 2026 at 12:05 pm

    You defined a class method, which is called on the class itself. What you need is an association extension. The method should be defined on your client model like this:

    class Client < ActiveRecord::Base
      has_many :invoices do
        def average_turnaround
          return map(&:turnaround).inject(:+) / count
        end    
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure this is possible, but in ruby, you can dynamically call a
Possible Duplicate: Ruby block and unparenthesized arguments I'm not sure I understand this syntax
I'm not sure if this is a rails method or a ruby method but
Not sure whats going on here, or what could be the integer in this
Not sure if this is the correct Stack Exchange website but here goes.. A
Not sure why I'm getting this, but I just installed RVM, the new Ruby
I'm not sure what to call this, so I'll give an example. In PHP
I'm not sure of the best idiom for C style call-backs in Ruby -
I see this a lot in many ruby scripts but not sure what it
Not sure how to do this in Ruby/Rails only thing I could come up

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.