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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:26:48+00:00 2026-06-17T15:26:48+00:00

I want to extend the Array returned from a query to add new methods.

  • 0

I want to extend the Array returned from a query to add new methods.

Lets assume I have a model Query and it has a few calculated attributes size_x, size_y …

In my controller, I retrieve a few queries

@queries = Query.limit(10).all

Now @queries becomes an Array. I would like to extend the Array with 2 methods

@queries.average_size_x

And

@queries.average_size_y

I am looking for a neat solution that will enhance the Array returned with those 2 methods without adding it to the global Array class. Is there any way to do that ?

Here are some things I tried:

  • Redefining the self.find method

This only works if I change my query to the old syntax

Query.find(:all, :limit => 5)
  • extending the relation

    Query.limit(10).extending(QueryAggregator)

This works if I don’t call the all method as the object is an ActiveRecord relation but onceall is called, the methods average_size_x is not available.

The only option I see is to extend Array class and add the methods there, but it might be irrelevant for other Models…

Any suggestions ?

  • 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-17T15:26:50+00:00Added an answer on June 17, 2026 at 3:26 pm

    This should works. You have a proxy instance and extending by module.

    query = Query.limit(10).all
    
    module SomeModule
      def average_size_x
      end
    
      def average_size_y
      end
    end
    
    query.extend(SomeModule)
    
    query.average_size_y
    query.average_size_y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an associative array of objects that I want to extend with a
I have a class PlaylistTrack that I want to extend class Song. When I
I have a Django template that I want to extend in multiple places. In
I have a SVG file that I want to extend by adding onclick handlers
Folks I have a sealed class as follows. I want to extend this sealed
So I want to extend for example this RecursiveIterator from the SPL with the
I want to extend core Array class with simple method: class Array def to_hash
I want to populate a collection from an json file, which has other data
I want to extend Array and Object like the following code: $(window).load(function() { Array.prototype.x
I have an array of string literals and I want to loop over them,

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.