# Shared Model Code

**URL:** <https://www.ruby-forum.com/t/shared-model-code/124792>\
**Category:** Rails\
**Created:** [January 2, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/shared-model-code/124792 "2008-01-02T18:35:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 2, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/shared-model-code/124792/1 "2008-01-02T18:35:46Z")

</div>

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?

Thanks,

-scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 2, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/shared-model-code/124792/2 "2008-01-02T18:42:27Z")

</div>

a module in the lib folder would be the right place i think  
then you can include it in the models

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 2, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/shared-model-code/124792/3 "2008-01-02T19:51:42Z")

</div>

in lib/my\_cool\_library.rb

module MyCoolLibrary

def my\_cool\_method  
puts “I do stuff that’s cool”  
end

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.new  
user.my\_cool\_method

-Brian
