Shared Model Methods

Hi all,

I just have a question about shared model methods. I have a method that
will be used for quite a few models in my application (once I've cleaned
it up a bit). Where can I place shared model methods? I know that
controllers can use helpers or even the application_controller. But,
what do models use for a shared mechanism? Is it config/initialize?

Yes, just use Ruby modules.

module SharedMethods

  def list
    # ...
  end
end

And in your model,

class MyModel < ActiveRecord::Base

extend SharedMethods

end

Does this help?

Jeff

Rails for Everyone: Oct 24, 2009 in Chicago:
http://www.purpleworkshops.com/workshops/rails-for-everyone

Thanks a lot Jeff - yeah that's what I'm looking for. Does the ruby
module have to belong in a certain place in my project to be usable by
other models?

I figured it out. I actually made it simpler for me since I will be
using a lot of methods (duplicate) for 37 different models.

I just created a plugin..

script/generate plugin template_searches

init.rb added..

ActiveRecord::Base.send :include, TemplateSearches

template_searches.rb added..

module TemplateSearches
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods

  My Methods...

  end
end

Removed the duplicate code and restarted and all is working well.