Extending models from engines

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 :slight_smile: 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

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

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

On 2/21/07, Peter B. [email protected] 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.

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

  • James

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs