Shared Model Code

Where does one stick shared model code? I have a method that I want to share between models. What file should I define that method in if I want to keep DRY?



in lib/my_cool_library.rb

module MyCoolLibrary

def my_cool_method puts “I do stuff that’s cool” end


Restart server

now, in your models, just do

class User < ActiveRecord::Base

include MyCoolLibrary end

This will “mix in” this code into your class, making it available to instance methods.

user = user.my_cool_method