ruby on rails - Application level filtering/data manipulation -


i have model many different children (has_many).

my app needs lots of different manipulation on set of data. therefore getting data database pretty simple, won't need use scopes , finders, want things on data equivalent say:

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

should writing equivalent methods manipulate served data? or in helper?

just need little things see relate querying actual database subset of data, when require children of 1 model, many different visualisations on it.

so, if understand right, you'd query whole set of data once, select different sets of rows different uses.

named scopes won't caching, building separate queries each variation.

if want simple, can query rows (activerecord cache result same query), can use select filter rows:

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

or, 1 step further, can create general method filters 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 want keep chainable method syntax provided scopes, add filter methods array class:

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

or, if don't want add garbage every array object, can add them objects, you'll need override all method , add methods every time you're creating subset of 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 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -