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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:13:45+00:00 2026-05-16T01:13:45+00:00

I have a model with many different children (has_many). My app needs to lots

  • 0

I have a model with many different children (has_many).

My app needs to lots of different manipulation on the who set of data. Therefore getting the data from the database is pretty simple, so I won’t really need to use scopes and finders, BUT I want to do things on the data that are equivalent to say:

named_scope :red, :conditions => { :colour => 'red' }
named_scope :since, lambda {|time| {:conditions => ["created_at > ?", time] }}

Should I be writing the equivalent methods that just manipulate the already served data? Or in a helper?

Just need a little help as most things I see all relate to querying the actual database for a subset of data, when I will require all the children of this one model, but do many different visualisations on it.

  • 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-16T01:13:46+00:00Added an answer on May 16, 2026 at 1:13 am

    So, if I understand right, you’d like to query the whole set of data once, then select different sets of rows from it for different uses.

    Named scopes won’t do any caching, as they are building separate queries for each variation.

    If you want a simple, you can just query all rows (ActiveRecord will cache the result for the same query), then you can use select to filter the rows:

    Article.all.select{|a| a.colour == 'red'}

    Or, one step further, you can create a general method that filters the rows based on parameters:

    def self.search(options)
      articles = Articles.all
      articles = articles.select{|a| a.color == 'red'} if options[:red]
      articles = articles.select{|a| a.created_at > options[:since]} if options[:since]
      articles
    end
    
    Article.search(:red => true, :since => 2.days.ago)
    

    Or, if you really want to keep the chainable method syntax provided by scopes, then add your filter methods to the Array class:

    class Array
    
      def red
        select.select{|a| a.colour == 'red'}
      end
    
    end
    

    Or, if you just don’t want to add all that garbage to every Array object, you can just add them to the objects, but you’ll need to override the all method and add the methods every time you’re creating a subset of the rows:

    def self.with_filters(articles)
    
      def articles.red
        Article.with_filters(select{|a| a.color == 'red'})
      end
    
      articles
    end
    
    def self.all
      self.with_filters(super)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some data which represents many model runs under different scenarios. For a
I have many different arrays with dictionaries of hotels in a data model class.
I have Product model and it has many categories with a has_many :through association
I have a parent model that has many child models, for example: App.Blog =
I have a lot of data that I want to disseminate to many different
Is it ok to have only one model that can hold many different types
I have a model with many fields (nearly 40). The client wants the fields
I have a model Task which will HABTM many TaskTargets. When it comes to
I have an Entry model which has many Tag s. Tag s are added
I have a model Whitelabel and a User has_many :whitelables I have a custom

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.