Use helper method in controller?

I created a helper method in person_helper.rb, and want to use this
method in person_controller#list, but I get the error “undefined method
`filter_sql’ for #PersonController:0x872dec4”.

Here is the helper class (person_helper.rb):

module PersonHelper
Sql = “SELECT * FROM People WHERE name LIKE ? OR address LIKE ?”
def filter_sql(name, address, city)
sanitize_sql([Sql, name.to_s + ‘%’, address.to_s + ‘%’])
end
end

Here is the relevant part of the controller class
(person_controller.rb):

class PersonController < ApplicationController
def list
sql = filter_sql(params[:name_val], params[:address_val])
@person_pages, @people = paginate Person.find_by_sql(sql), per_page
=> 50
end
end

Any ideas on what I am doing wrong? Thanks in advance for your help!

I would highly recommend you push this back into your model if you need
it
in both places. This seems like business logic, which belongs in the
model.

class Person < ActiveRecord::Base

… your other stuff

def self.filter_by_name_and_address(name, address)

   name = "#{name}%"
   address = "#{address}%"
   self.find(:all, conditions=>["name like ? and address like ?", 

name,
address

end

end

Your controller might be like this now:

def list
@person_pages, @people = paginate
Person.filter_by_name_and_address(params[:name],
params[:address]), per_page => 50
end

That’s untested, btw, but it should get you going in the right
direction.
Helpers are really for code that is directly related to views and
controllers. You want to try to keep as much db stuff in the model as
you
can. You might have a compelling reason to use the helper as you’d
originally intended, so my apologies if this is not suitable for you.

Your controller might be like this now:

def list
@person_pages, @people = paginate
Person.filter_by_name_and_address(params[:name],
params[:address]), per_page => 50
end

Putting this in the model class is good advice. I am having a problem
in getting this to work, however, looking for a bit more help…

Here is my model list method (stripped down to isolate my problem):

def list
@person_pages, @people = paginate Person.find(:all, :conditions =>
“lastname=‘test’”), :per_page => 50
end

which results in this exception:
“#Person:0x85f9d14#Person:0x85f9b5c#Person:0x85f9b34#Person:0x85f9b0c#Person:0x85f9ae4#Person:0x85f9abc#Person:0x85f9a94#Person:0x85f9a6c
is not a valid constant name!

Apparently using Person.find (like the scaffold show method) doesn’t
work with paginate??

Any ideas? This seems so simple and is undoubtedly a stupid error on my
part, but I just don’t see it…

Yes, sorry… that was a mistake on my end. I should have tested my
code
before I sent it to you. The built-in paginator is kinda ugly, doesn’t
perform well. I’d recommend the will_paginate plugin. It will let you
do
what you want to do. You are correct that the paginate method doesn’t
let
you do what you want.

I’d recommend the will_paginate plugin. It will let you
do what you want to do.

My last example was stripped down, I will still need to use a
find_by_sql method. I see this is supported with will_paginate, but the
:total_entries parameter must be explicitly provided. Does this mean I
would have to run a query first to get/provide the total? This would
also mean that I would have to re-retrieve this value when a row is
added/deleted but I guess that makes sense since the pages wouldn’t be
right anymore anyway…