# Proper way to extend an app engine model

**URL:** <https://www.ruby-forum.com/t/proper-way-to-extend-an-app-engine-model/166900>\
**Category:** Rails\
**Created:** [April 30, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/proper-way-to-extend-an-app-engine-model/166900 "2009-04-30T17:56:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brent\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b9bd4f/32.png) [@Brent\_D](https://www.ruby-forum.com/u/Brent_D)\
**Post date:** [April 30, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/proper-way-to-extend-an-app-engine-model/166900/1 "2009-04-30T17:56:33Z")

</div>

Can anyone tell me the “proper” way to extend a model provided by an  
app engine (or any plugin for that matter).

I’m attempting to extend an app engine model by adding a  
has\_many :through. So I’ve got lib/myplugin\_ext.rb and I’m requiring  
that file in evironment.rb. My extension uses class\_eval to add the  
association.

This works fine in the console for viewing and adding stuff to the  
has\_many :through association, but in dev mode – after the first  
request – if I try to add to the has\_many :through, I get “TypeError:  
Can’t dup NilClass”.

Because it works in the console but not in development mode after the  
first request, this leads me to believe that it’s a weird problem with  
the reloader and how I’m extending the class. I’ve tried virtually  
everything I can think of (aside from just moving the models from the  
app engine into my app directory and modifying it there, which  
works…), but I can’t seem to avoid this error any other way.

I tried the “unloadable” suggestion posted here: [Can't dup NilClass… maybe try `unloadable` – Daniel X's Blog](http://strd6.com/?p=250)

Adding unloadable to the app engine model only seemed to make the  
situation worse. The app engine model gets reloaded without the  
extension at all on subsequent requests.

I isolated the problem in a blank app and posted it here on github:  
[http://github.com/brentd/cantdupnilclass/tree/master](http://github.com/brentd/cantdupnilclass/tree/master)

The stack trace is also available at that repo.

If I’m doin’ it completely wrong, please tell me what would help. Much  
appreciated 🙂

Brent

---

<div class="post-metadata">

**Author:** ![Brent\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b9bd4f/32.png) [@Brent\_D](https://www.ruby-forum.com/u/Brent_D)\
**Post date:** [May 7, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/proper-way-to-extend-an-app-engine-model/166900/2 "2009-05-07T13:06:26Z")

</div>

Brent D. wrote:

> Can anyone tell me the “proper” way to extend a model provided by an  
> app engine (or any plugin for that matter).

I _think_ that you can do it in one of these two ways:

First, you could put something like this in your pluginâ€™s init.rb file:

# This plugin should be reloaded in development mode.

if RAILS\_ENV == â€˜developmentâ€™  
ActiveSupport::Dependencies.load\_once\_paths.reject!{|x| x =~  
/^#{Regexp.escape(File.dirname( **FILE** ))}/}  
end

Second, you could put something like this in your applicationâ€™s  
environment.rb file:  
config.reload\_plugins = true if RAILS\_ENV == â€˜developmentâ€™

---

<div class="post-metadata">

**Author:** ![Brent\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b9bd4f/32.png) [@Brent\_D](https://www.ruby-forum.com/u/Brent_D)\
**Post date:** [July 1, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/proper-way-to-extend-an-app-engine-model/166900/3 "2009-07-01T22:29:43Z")

</div>

I am experiencing the same problems as Brent: when a plugin extends an  
application model, things work as expected in production mode while  
development mode is buggy.  
I tried both of Paul’s suggestions, but still no success …

-Anders

---

<div class="post-metadata">

**Author:** ![Brent\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b9bd4f/32.png) [@Brent\_D](https://www.ruby-forum.com/u/Brent_D)\
**Post date:** [August 13, 2009, 10:11am UTC](https://www.ruby-forum.com/t/proper-way-to-extend-an-app-engine-model/166900/4 "2009-08-13T10:11:46Z")

</div>

I am also experiencing these problems, any news on this?

Thanks.

---

<div class="post-metadata">

**Author:** ![Brent\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b9bd4f/32.png) [@Brent\_D](https://www.ruby-forum.com/u/Brent_D)\
**Post date:** [August 13, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/proper-way-to-extend-an-app-engine-model/166900/5 "2009-08-13T12:06:32Z")

</div>

The problem is that your extensions (in lib/blog\_engine\_ext.rb) are  
only being loaded once, at the bottom of environment.rb (the require  
statement).

When the system reloads, there’s nothing to trigger the re-inclusion  
of your code, and so the Post model ends up in a bit of a freaky  
state.

The solution is simple; any kind of functionality injection should be  
performed in a config.to\_prepare block. This goes for the contents of  
init.rb, along with the approach that you’re taking.

I’ve changed environment.rb as below, and the app now works for every  
subsequent request:

```
Rails::Initializer.run do |config|
  # etc
  config.to_prepare do
    # use load to ensure that the file is actually evaluated every

```

time  
load ‘blog\_engine\_ext.rb’  
end  
end

```
# no require needed.

```

Hope that helps,

- James
