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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brent D. (Guest)
on 2009-04-30 19: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
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: 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

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
Pau C. (Guest)
on 2009-05-07 15:06
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’
Anders Jacobsen (Guest)
on 2009-07-02 00: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 ...

-Anders
Oriol Gual (Guest)
on 2009-08-13 12:11
(Received via mailing list)
I am also experiencing these problems, any news on this?

Thanks.
James A. (Guest)
on 2009-08-13 14: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
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
This topic is locked and can not be replied to.