Could not get to it yesterday due to other work. Am still struggling
with it. There is something basic that I do not understand. I have
looked at acts_as plugins (list and tree for instance). I am afraid
that is not going to help me. Here is the thing that I do not get:
1. The Rails lib directory contains reusable code that can be used
anywhere in that particular Rails application. Following that basic
concept, I created a file called active_records_extensions.rb in it
which has the following slightly modified code from the previous
posting:
module ActiveRecord
class Base
class << self
def search(search, current_page)
if search.blank?
WillPaginate::Finder::paginate(:all, :page => current_page ||
1, :per_page => 5)
else
WillPaginate::Finder::paginate(:all, :conditions => ["name
LIKE ?", "%#{search}%"], :order => 'name',
:page => current_page || 1, :per_page => 5)
end
end
end
end
end
Now search is supposed to be a class method since it is called from the
index method of a controller like this:
def index
@pizzas = Pizza.search(params[:search], params[:page])
end
That is why I am defining the search method as a class method for
ActiveRecord::Base class as shown above. I have done this sort of thing
many times before without any problems. The twist this time is that the
"Search" method is calling the paginate class method of will_paginate
plugin.
First, I tried requiring the will_paginate class method, but that did
not work. Next, I tried giving the full path for the paginate class
method as shown above in the code. Still, I get the same error. Here
is the stack trace snippet.
NoMethodError (undefined method `search' for #<Class:0xb7143b6c>):
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1672:in
`method_missing_without_paginate'
/vendor/plugins/will_paginate/lib/will_paginate/finder.rb:170:in
`method_missing'
/app/controllers/pizzas_controller.rb:17:in `index'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1166:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1166:in
`perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:579:in
`call_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:572:in
`perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
I am hoping someone can help me understand what is it that I am missing.
I am doing this project to further my knowledge about designing reusable
code in Rails and there is something fundamental that I am not able to
grasp.
As always, thanks for your time.
Bharat