Forum: Ruby on Rails Newbie Question: The model equivilent of "helpers"

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
ljw1001 (Guest)
on 2005-11-25 18:35
(Received via mailing list)
If I have ruby code that I need to share among a number of model
what's the "right" place to put it form a Ruby standpoint?

The code provides support for various calculations.  It has no data
that are persisted in a database.  It seems like there should be
like a helpers directory for models for this kind of supporting code.

thanks for your help.
james.adam (Guest)
on 2005-11-25 18:39
(Received via mailing list)
If you have methods that you want to make available in a number of
classes, you should put these methods in a module and have each class
'include' this module, i.e.

in RAILS_ROOT/lib/my_module.rb (you'll want to 'require' this file in
environment.rb too, probably)

  module MyModule
     def calculate_meaning_of_life
        6 * 7

... then in your model objects:


  class ModelAlpha <  ActiveRecord::Base
    include MyModule


  class ModelBeta < ActiveRecord::Base
    include MyModule

... and so on.

- james
This topic is locked and can not be replied to.