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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:26:49+00:00 2026-05-19T09:26:49+00:00

I was wondering if it’s possible to do something like follows: Let’s say I

  • 0

I was wondering if it’s possible to do something like follows:

Let’s say I have a Rails model, Foo, with a database attribute value. Foo belongs_to Bar, Bar has_many Foos.

In my model, I’d like to do something like:

class Foo < ActiveRecord::Base

  belongs_to :bar

  def self.average
    # return the value of all foos here
  end

end

Ideally I’d like to have this method return a value that matched the scope from which it was called, so that:

Foo.average # would return the average value of all foos

@bar = Bar.find(1)
@bar.foos.average # would return the average of all foos where bar_id == 1

Can such a thing be done, and if so, how? 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-05-19T09:26:50+00:00Added an answer on May 19, 2026 at 9:26 am

    What you have will work as is, as long as you make sure to call methods on self instead of Foo in the body of the average method. When calling methods on a scope of Foo, self in the body of that method will be assigned to the scope object rather than Foo. Here’s a slightly more concrete example:

    # app/models/club.rb
    class Club < ActiveRecord::Base
      # name:string
      has_many :people
    end
    
    # app/models/person.rb
    class Person < ActiveRecord::Base
      # club_id:integer, name:string, age:integer
      belongs_to :club
    
      def self.average_age
        # note that sum and count are being called on self, not Person
        sum('age') / count
      end
    end
    

    Let’s see what happens when we create some clubs and people:

    $ rails console
    Loading development environment (Rails 3.0.3)
    irb(main):001:0> boys_club = Club.create(:name => 'boys')
    irb(main):002:0> girls_club = Club.create(:name => 'girls')
    irb(main):003:0> boys_club.people.create(:name => 'bob', :age => 20)
    irb(main):004:0> boys_club.people.create(:name => 'joe', :age => 22)
    irb(main):005:0> girls_club.people.create(:name => 'betty', :age => 30)
    irb(main):006:0> Person.average_age
    => 24
    irb(main):007:0> boys_club.people.average_age
    => 21
    irb(main):008:0> Person.where("name LIKE 'b%'").average_age
    => 25
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if it is possible to have a version control of a MySQL database.
Wondering if it's possible to process HAML from database entries. My model currently processes
Wondering if this is possible or something with this effect. public class MyModel {
Wondering if it's possible to change a value returned from a queryset before sending
Wondering if someone can answer something that has stumped me. I have a Timer
Wondering if someone could help me. I have next to no knowledge with Ajax,
Wondering what is the best or most popular database client tool. Similar to Microsoft's
Wondering, if possible, how I would go about sorting entities from an NSSet by
wondering how to carry out this requirement if at all possible. I am modifying
Wondering if it is possible for my claims aware application (ASP.NET) to save a

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.