# Share model definition

**URL:** <https://www.ruby-forum.com/t/share-model-definition/63860>\
**Category:** Rails\
**Created:** [June 19, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/share-model-definition/63860 "2006-06-19T23:03:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [June 19, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/share-model-definition/63860/1 "2006-06-19T23:03:57Z")

</div>

I want to share everything about a model except the db connection. Is  
this a reasonable way to do it?

module NotebookDefinition

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [June 19, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/share-model-definition/63860/2 "2006-06-19T23:14:51Z")

</div>

IF everything is the same except for the dbconnection, you could just  
specify the dbconnection you want in each model, or put that in a module  
that you include.

> From a plugin of mine where the model and the database.yml file are in the  
> same folder:

class PersonRecord \<\< ActiveRecord::Base

```
c = YAML::load(File.open(File.dirname( __FILE__ ) +

```

“/person\_database.yml”))

```
PersonRecord.establish_connection(
  :adapter=>c[RAILS_ENV]['adapter'],
  :host=>c[RAILS_ENV]['host'],
  :username=>c[RAILS_ENV]['username'],
  :password=>c[RAILS_ENV]['password'],
  :database=>c[RAILS_ENV]['database']
)

```

end

You could use modules to put the common code into multiple classes, but  
you  
could also make a new base class and put the common code in that

class MyBase \< ActiveRecord::Base  
def foo  
"bar’  
end  
end

class User \< MyBase  
has\_many :projects  
c = YAML::load(File.open(File.dirname( **FILE** ) +  
“/user\_database.yml”))

```
User.establish_connection(
  :adapter=>c[RAILS_ENV]['adapter'],
  :host=>c[RAILS_ENV]['host'],
  :username=>c[RAILS_ENV]['username'],
  :password=>c[RAILS_ENV]['password'],
  :database=>c[RAILS_ENV]['database']
)

```

end

Would that work?

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [June 19, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/share-model-definition/63860/3 "2006-06-19T23:18:00Z")

</div>

sorry… fat-fingered… I want to share a common definition for each of  
the classes, but have all classes within a given module be able to  
operate on a separate database from other modules…

module NoteBookDefinition  
def import\_definition  
set\_table\_name :notebooks  
has\_many :pages  
end  
end

class Master::Notebook  
extend NotebookDefinition  
import\_definition  
end

class Other::Notebook  
extend NotebookDefinition  
import\_definition  
establish\_connection :otherdb  
end

module PageDefinition  
def import\_definition  
belongs\_to :notebook  
end  
end

class Master::Page  
extend PageDefinition  
import\_definition  
end

class Other::Page  
extend PageDefinition  
import\_definition  
establish\_connection :otherdb  
end

n = Master::Notebook.find :first  
n.class (returns Master::Notebook)  
n.pages[0].class (returns Master::Page)

m = Other::Notebook.find :first  
m.class (returns Other::Notebook)  
m.pages[0].class (returns Other::Page)

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [June 19, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/share-model-definition/63860/4 "2006-06-19T23:52:24Z")

</div>

Thanks for the suggestion, Brian… If I specify the dbconnection in  
each model, then I’d have to repeat the entire model definition… The  
question is, how to best share everything _except_ the dbconnection…  
It took a while to come up with the above scheme to share the rest  
(relationships, validations, etc) Is that the best way to do it?  
-dan

Brian H. wrote:

> IF everything is the same except for the dbconnection, you could just  
> specify the dbconnection you want in each model, or put that in a module  
> that you include.
> 
> > From a plugin of mine where the model and the database.yml file are in the  
> > same folder:
> 
> class PersonRecord \<\< ActiveRecord::Base
> 
> ```
> c = YAML::load(File.open(File.dirname( __FILE__ ) +
> 
> ```
> 
> “/person\_database.yml”))
> 
> ```
> PersonRecord.establish_connection(
> :adapter=>c[RAILS_ENV]['adapter'],
> :host=>c[RAILS_ENV]['host'],
> :username=>c[RAILS_ENV]['username'],
> :password=>c[RAILS_ENV]['password'],
> :database=>c[RAILS_ENV]['database']
> )
> 
> ```
> 
> end
> 
> You could use modules to put the common code into multiple classes, but  
> you  
> could also make a new base class and put the common code in that
> 
> class MyBase \< ActiveRecord::Base  
> def foo  
> "bar’  
> end  
> end
> 
> class User \< MyBase  
> has\_many :projects  
> c = YAML::load(File.open(File.dirname( **FILE** ) +  
> “/user\_database.yml”))
> 
> ```
> User.establish_connection(
> :adapter=>c[RAILS_ENV]['adapter'],
> :host=>c[RAILS_ENV]['host'],
> :username=>c[RAILS_ENV]['username'],
> :password=>c[RAILS_ENV]['password'],
> :database=>c[RAILS_ENV]['database']
> )
> 
> ```
> 
> end
> 
> Would that work?

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [August 3, 2006, 11:57am UTC](https://www.ruby-forum.com/t/share-model-definition/63860/5 "2006-08-03T11:57:19Z")

</div>

Dan K. wrote:

> I want to share everything about a model except the db connection. Is  
> this a reasonable way to do it?
> 
> module NotebookDefinition

Please expound on ‘share’. Are we talking subclassing? If so, any model  
can redefine the AR’s connection, and all of that class will use the  
same connection. Otherwise I’m not sure what you’re hinting at.

Jason
