Forum: Ruby on Rails Proper way to extend an app engine model

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E71822bf2365c34214d8e69bb3b432f3?d=identicon&s=25 Brent Dillingham (Guest)
on 2009-04-30 17:56
(Received via mailing list)
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

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:

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:

The stack trace is also available at that repo.

If I'm doin' it completely wrong, please tell me what would help. Much
appreciated :)

90e140f8553ecdec1b0d7dbf608a0561?d=identicon&s=25 Pau Cor (phylae)
on 2009-05-07 13:06
Brent Dillingham 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 =~

Second, you could put something like this in your application’s
environment.rb file:
config.reload_plugins = true if RAILS_ENV == ‘development’
E49b31434171afee0161fb38ed317484?d=identicon&s=25 Anders Jacobsen (Guest)
on 2009-07-01 22:29
(Received via mailing list)
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 ...

E4679be8d7d2484f9c0b8c4df71f4d2a?d=identicon&s=25 Oriol Gual (Guest)
on 2009-08-13 10:11
(Received via mailing list)
I am also experiencing these problems, any news on this?

Acd62030df551952268e84c8fff26a5b?d=identicon&s=25 James Adam (lazyatom)
on 2009-08-13 12:06
(Received via mailing list)
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

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

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: do |config|
      # etc
      config.to_prepare do
        # use load to ensure that the file is actually evaluated every
        load 'blog_engine_ext.rb'

    # no require needed.

Hope that helps,

- James
This topic is locked and can not be replied to.