Refactoring into Model...need help

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

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…

On Aug 20, 9:28 pm, ÜberBaller [email protected] 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

On Aug 20, 3:28 pm, Frederick C. [email protected]
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” }
]
) %>