# DRY concepts for Models

**URL:** <https://www.ruby-forum.com/t/dry-concepts-for-models/170145>\
**Category:** Rails\
**Created:** [June 12, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/dry-concepts-for-models/170145 "2009-06-12T21:21:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 12, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/dry-concepts-for-models/170145/1 "2009-06-12T21:21:29Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 13, 2009, 1:50am UTC](https://www.ruby-forum.com/t/dry-concepts-for-models/170145/2 "2009-06-13T01:50:07Z")

</div>

Ã„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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 13, 2009, 4:41am UTC](https://www.ruby-forum.com/t/dry-concepts-for-models/170145/3 "2009-06-13T04:41:58Z")

</div>

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.
