DRY concepts for Models

I will have roughly 37 models that will contain this piece of code
(although it will be further modified):

def self.search(search, page)
start_date = Time.now.beginning_of_week
end_date = Time.now.end_of_week
paginate :per_page => 10, :page => page,
:conditions => [‘compiled_on > ? and compiled_on < ?’, start_date,
end_date],
:conditions => [‘name like ?’, “%#{search}%”],
:order => ‘rank’
end

It defines pagination using will_paginate.

The question I have is how can I simplify this by creating a
global/helper class that can be used in the other 37 pages. I’m still
very newbish but I’m really trying to learn best practice concepts so my
cleanup process is DRY as I develop.

Many thanks…

Älphä Blüë wrote:

I will have roughly 37 models

Really? That’s a lot of models – I wonder whether it’s necessary to
have so many. (If your data is complex, perhaps it is necessary.)

that will contain this piece of code
(although it will be further modified):

[…]

The question I have is how can I simplify this by creating a
global/helper class that can be used in the other 37 pages.
[…]

Sure. Put the code in a module that gets included in all the models, or
subclass ActiveRecord::Base.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks Marnen,

Yeah the code is complex because it’s iterating over many different
types of tables that return numerous headers and results. I’ll do what
you suggested.