Application level filtering/data manipulation
I have a model with many different children (has_many)开发者_StackOverflow社区.
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.
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
精彩评论