On Mar 23, 2009, at 5:09 PM, Bharat Ruparel wrote:
>
> I am trying to monkey-patch the ActiveRecord::Base class to
> incorporate
> a generic search class method so that it can be used by all model
> classes which need this functionality. Since model classes directly
> inherit from ActiveRecord::Base and unlike controllers and helpers, do
> not have an ancestor class defined, I think I am forced to open the
> ActiveRecord::Base class and patch it? May be I am wrong. Anyway,
> here
Why not create your method as a module and then include it into the
classes you need it? Look at any of the plugins out there that are of
the "acts_as_xxxx" variety which add methods to the model they are
called from... just follow those examples and you shouldn't need to
directly modify AR::Base like this.
> is my code in the active_record_extensions.rb file that I created in
> the
> RAILS_ROOT/lib directory:
>
> module ActiveRecord
>
> class Base
> def self.search(search, current_page)
> if search.blank?
> paginate(:all, :page => current_page || 1, :per_page => 5)
> else
> paginate(:all, :conditions => ["name LIKE ?", "%#{search}%"],
> :order => 'name',
> :page => current_page || 1, :per_page => 5)
> end
> end
> end
>
> end
>
> Note that paginate method comes from the will_paginate plugin
> installed
> in vendor/plugins directory.
>
> This does not work. My program is having trouble with the paginate
> method from will_paginate plugin being called in the code shown above?
>
> This seems to be a load_path issue? One way to solve this is to
> install
> will_paginate as a gem and then require it before calling paginate?
> But
> is there a way to make with work as a plugin as I have it currently
> for
> creating this generic routine?
>
> Thanks for your time in advance.
>
> Bharat
> --
> Posted via http://www.ruby-forum.com/.
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---