Älphä Blüë wrote:
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?
Can someone explain a quick process for it?
Depending on how big the sharing ends up (my app is about 85% shared
functionality among models), you can always create an abstract model
class that defines those capabilities, then inherit concrete models from
class GenericModel < ActiveRecord::Base
self.abstract_class = true
# generic implementations shared by all models
# my app has some 30 'generic' model methods, relationship
# management, cache management, pdf generation, etc
class Project < GenericModel
# project-specific methods
# project has only 5, 2 of which override GenericModel
You can do the same thing with controllers as well...
GenericController has all the common stuff (index, show, edit, etc, etc)
ProjectsController has just the project specific stuff.
Whatever works for you.