# Extending models from engines

**URL:** <https://www.ruby-forum.com/t/extending-models-from-engines/86324>\
**Category:** Engines Development\
**Created:** [February 21, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/extending-models-from-engines/86324 "2007-02-21T14:32:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 21, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/extending-models-from-engines/86324/1 "2007-02-21T14:32:38Z")

</div>

Hi there,

We’ve been busy on writing our own little engine, and we didn’t want to  
put our engine’s model code in a lib or make it an abstract class, since  
that isn’t very clean 🙂 Of course, copying the model code completely  
into  
the application’s model is right out.

While thinking about this, it occurred to me that it’s very easy to let  
Ruby  
override the engine’s model:

In RAILS\_ROOT/app/models/foo.rb:

require RAILS\_ROOT + “/vendor/plugins/my\_engine/app/models/foo”

class Foo \< ActiveRecord::Base  
def bar  
qux()  
end  
end

This simply works. When you call Foo.new.blah(), it calls the engine’s  
model’s qux() function. Perhaps this should be added to the  
documentation as  
this is a lot cleaner (IMHO) than the other two suggestions that are in  
the  
documentation?

Regards,  
Peter B.  
Solide ICT - [http://www.solide-ict.nl](http://www.solide-ict.nl)

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 21, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/extending-models-from-engines/86324/2 "2007-02-21T15:55:46Z")

</div>

Hi there,

Oddly enough, now it appears to work. I don’t know what changed,  
unfortunately. We moved a lot of code around and made some major  
changes, so it might have accidentally been fixed. Thanks for the  
reply, anyway.

Regards,  
Peter B.  
Solide ICT - [http://www.solide-ict.nl](http://www.solide-ict.nl)

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 22, 2007, 9:02am UTC](https://www.ruby-forum.com/t/extending-models-from-engines/86324/3 "2007-02-22T09:02:45Z")

</div>

On Wed, Feb 21, 2007 at 03:56:27PM +0100, Peter B. wrote:

> Hi there,
> 
> Oddly enough, now it appears to work. I don’t know what changed,  
> unfortunately. We moved a lot of code around and made some major  
> changes, so it might have accidentally been fixed. Thanks for the  
> reply, anyway.

Hmm, this should’ve been a reply to my other thread about testing.  
Sorry for the noise.

Regards,  
Peter B.  
Solide ICT - [http://www.solide-ict.nl](http://www.solide-ict.nl)

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 23, 2007, 4:46am UTC](https://www.ruby-forum.com/t/extending-models-from-engines/86324/4 "2007-02-23T04:46:31Z")

</div>

On 2/21/07, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> While thinking about this, it occurred to me that it’s very easy to let Ruby  
> override the engine’s model:
> 
> In RAILS\_ROOT/app/models/foo.rb:
> 
> require RAILS\_ROOT + “/vendor/plugins/my\_engine/app/models/foo”
> 
> class Foo \< ActiveRecord::Base  
> end

I knew there had to be a better way. I just started monkeying around  
with this myself but I’m pleased you beat me to it. You would think  
that someone would have thought of this before though. Maybe there’s  
some hidden risk that you and I are both missing?

-Chuck V.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 23, 2007, 11:18am UTC](https://www.ruby-forum.com/t/extending-models-from-engines/86324/5 "2007-02-23T11:18:12Z")

</div>

There’s no drawback - you’re manually doing what the ‘code mixing’  
process for controllers and helpers does automatically 🙂

- James

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 26, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/extending-models-from-engines/86324/6 "2007-02-26T17:12:18Z")

</div>

Any idea why putting:

include Reloadable

in the class would hose things up? The idea below works great but I  
have to  
restart webbrick/mongrel every time I make a change. If I put in  
“include  
Reloadable” it seems to break everything.

-ben
