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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:45:39+00:00 2026-06-06T02:45:39+00:00

I have a set of rows which I’ve fetched from a table. Let’s say

  • 0

I have a set of rows which I’ve fetched from a table. Let’s say the object Rating. After fetching this object, I have say 100 entries from the database.

The Rating object might look like this:

table_ratings
t.integer :num

So what I now want to do is perform some calculations on these 100 rows without performing any other queries. I can do this, running an additional query:

r = Rating.all
good = r.where('num = 2') # this runs an additional query
"#{good}/#{r.length}"

This is a very rough idea of what I want to do, and I have other more complex output to build. Let’s imagine I have over 10 different calculations I need to perform on these rows, and so aliasing these in a sql query might not be the best idea.

What would be an efficient way to replace the r.where query above? Is there a Ruby method or a gem which would give me a similar query api into ActiveRecord Association collection objects?

  • 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-06T02:45:41+00:00Added an answer on June 6, 2026 at 2:45 am

    Rating.all returns an array of all Rating objects. From there, shift your focus to selecting and mapping from the array. eg.:

    @perfect_ratings = r.select{|x| x.a == 100}
    

    See:

    http://www.ruby-doc.org/core-1.9.3/Array.html

    ADDITIONAL COMMENTS:

    Going over the list of methods available for array, I find myself using the following frequently:

    To check a variable against multiple values:

    %w[dog cat llama].include? @pet_type  # returns true if @pet_type == 'cat'
    

    To create another array(map and collect are aliases):

    %w[dog cat llama].map(|pet| pet.capitalize)  #  ["Dog", "Cat", "Llama"]
    

    To sort and drop duplicates:

    %w[dog cat llama dog].sort.uniq  # ["cat", "dog", "llama"]
    

    << to add an element, + to add arrays, flatten to flatten embedded arrays into a single level array, count or length or size for number of elements, and join are the others I tend to use a lot.

    Finally, here is an example of join:

    %w[dog cat llama].join(' or ')  # "dog or cat or llama"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of data from a table ( TableA ) which relates
Let's say I have a table of two columns an three rows. Now, In
I have a scenario where I iterate over a set of rows from a
I have 2 tables set up like this (irrelevant rows omitted): > product -
I have set up 2 servers which are linked. From my LOCAL SERVER, I
I have a set of 100 rows, pretty similar to values which can be
I have a database table with 10,000,000+ rows which I process to create a
I have created a PHP script which builds a table from an XML document
I have a MySQL table which contains comma-separated values like this: first row=(3,56,78,12) second
I have set up a UITableView which has 3 sections in, with 3 rows

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.