# Refactoring into Model...need help

**URL:** <https://www.ruby-forum.com/t/refactoring-into-model-need-help/174113>\
**Category:** Rails\
**Created:** [August 20, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/refactoring-into-model-need-help/174113 "2009-08-20T23:50:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSSSberBaller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@SSSSSberBaller](https://www.ruby-forum.com/u/SSSSSberBaller)\
**Post date:** [August 20, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/refactoring-into-model-need-help/174113/1 "2009-08-20T23:50:13Z")

</div>

I posted this on the 2dcjqgrid group but maybe I’m better off here.

[http://groups.google.com/group/2dcjqgrid/browse\_thread/thread/ad4115ed1bca2050](http://groups.google.com/group/2dcjqgrid/browse_thread/thread/ad4115ed1bca2050)

I’m fairly new to rails and am having a bit of trouble moving my  
jqgrid code from the controller to the model. It worked fine in the  
controller. I know that the model can’t use params so I’m passing  
those into a method, but when I look at the sql query it is not  
passing all the arguements. TIA for any help.

class OligosController \< ApplicationController  
def index  
oligos = Oligo.search( params[:freezer], params[:cane],  
params  
[:box], params[:boxname], params[:slot], params[:oligoname], params  
[:sequence], params[:page], params[:sidx], params[:sord], params  
[:rows], params[:\_search] )  
respond\_to do |format|  
format.html  
format.json { render :json =\>  
oligos.to\_jqgrid\_json  
([:id,:freezer,:cane,:box,:boxname,:slot,:oligoname,:sequence],

params  
[:page], params[:rows], oligos.total\_entries) }  
end  
end  
end

class Oligo \< ActiveRecord::Base  
def self.search(freezer, cane, box, boxname, slot, oligoname,  
sequence, page, sidx, sord, rows, \_search)  
find(:all) do  
if \_search == “true”  
freezer =~ “%#{freezer}%” if freezer.present?  
cane =~ “%#{cane}%” if cane.present?  
box =~ “%#{box}%” if  
box.present?  
boxname =~ “%#{boxname}%” if boxname.present?  
slot =~ “%#{slot}%” if slot.present?  
oligoname =~ “%#{oligoname}%” if oligoname.present?  
sequence =~ “%#{sequence}%” if sequence.present?  
end  
paginate :page =\> page, :per\_page =\> rows  
order\_by “#{sidx} #{sord}”  
end  
end  
end

The original code can be found here…

> **[Home 1](https://thenewsrecorder.com/)**
>
>   NEWSFLASH 10 Best Ozzy Osbourne Songs of All Time – The News Recorder 1 week ago 10 Best La

---

<div class="post-metadata">

**Author:** ![SSSSSberBaller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@SSSSSberBaller](https://www.ruby-forum.com/u/SSSSSberBaller)\
**Post date:** [August 21, 2009, 12:30am UTC](https://www.ruby-forum.com/t/refactoring-into-model-need-help/174113/2 "2009-08-21T00:30:00Z")

</div>

On Aug 20, 9:28 pm, ÜberBaller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> slot =~ "%#{slot}%" if slot.present?
> oligoname =~ "%#{oligoname}%" if oligoname.present?
> sequence =~ "%#{sequence}%" if sequence.present?
> end
> 
> ```

What is this code suppose to do ? The code above isn’t actually doing  
anything as far as I can tell - just comparing pairs of strings with  
the =~ method (which as far as I can tell will just raise an exception  
because it’s expecting something more like a RegExp).  
If you’re trying to build up a set of conditions I’d look at  
named\_scope (either creating scopes for you different criteria and  
chaining them as appropriate or by using scoped )

Fred

---

<div class="post-metadata">

**Author:** ![SSSSSberBaller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@SSSSSberBaller](https://www.ruby-forum.com/u/SSSSSberBaller)\
**Post date:** [August 21, 2009, 7:56am UTC](https://www.ruby-forum.com/t/refactoring-into-model-need-help/174113/3 "2009-08-21T07:56:02Z")

</div>

On Aug 20, 3:28 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What is this code suppose to do ? The code above isn’t actually doing  
> anything as far as I can tell - just comparing pairs of strings with  
> the =~ method (which as far as I can tell will just raise an exception  
> because it’s expecting something more like a RegExp).  
> If you’re trying to build up a set of conditions I’d look at  
> named\_scope (either creating scopes for you different criteria and  
> chaining them as appropriate or by using scoped )
> 
> Fred

Here is the example code I was basing this off of.

def index  
users = User.find(:all) do  
if params[:\_search] == “true”  
pseudo =~ “%#{params[:pseudo]}%” if params[:pseudo].present?  
firstname =~ “%#{params[:firstname]}%” if params  
[:firstname].present?  
lastname =~ “%#{params[:lastname]}%” if params  
[:lastname].present?  
email =~ “%#{params[:email]}%” if params[:email].present?  
role =~ “%#{params[:role]}%” if params  
[:role].present?  
end  
paginate :page =\> params[:page], :per\_page =\> params[:rows]  
order\_by “#{params[:sidx]} #{params[:sord]}”  
end

respond\_to do |format|  
format.html  
format.json { render :json =\> users.to\_jqgrid\_json  
([:id,:pseudo,:firstname,:lastname,:email,:role],  
params[:page],  
params[:rows], users.total\_entries) }  
end  
end

\<%= jqgrid(“Football Players”, “players”, “/users”,  
[  
{ :field =\> “id”, :label =\> “ID”, :width =\> 35, :resizable =\>  
false },  
{ :field =\> “pseudo”, :label =\> “Pseudo” },  
{ :field =\> “firstname”, :label =\> “Firstname” },  
{ :field =\> “lastname”, :label =\> “Lastname” },  
{ :field =\> “email”, :label =\> “Email” },  
{ :field =\> “role”, :label =\> “Role” }  
]  
) %\>
