# Adding belongs\_to to an application model from an engine

**URL:** <https://www.ruby-forum.com/t/adding-belongs-to-to-an-application-model-from-an-engine/152142>\
**Category:** Engines\
**Created:** [October 30, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/adding-belongs-to-to-an-application-model-from-an-engine/152142 "2008-10-30T14:35:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![raphael\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@raphael\_b](https://www.ruby-forum.com/u/raphael_b)\
**Post date:** [October 30, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/adding-belongs-to-to-an-application-model-from-an-engine/152142/1 "2008-10-30T14:35:46Z")

</div>

Hi,

I have an Instance model in my application, to which I want to add a  
has\_many :web\_file\_attachment from the plugin.  
I tried putting this in my plugin’s app/model/instance.rb :  
class Instance \< ActiveRecord::Base  
has\_many :web\_file\_attachments , :dependent =\> :destroy  
end

but it doesn’t seem to work.

What’s the way to get this working?

Thanks

Raph

---

<div class="post-metadata">

**Author:** ![raphael\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@raphael\_b](https://www.ruby-forum.com/u/raphael_b)\
**Post date:** [October 30, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/adding-belongs-to-to-an-application-model-from-an-engine/152142/2 "2008-10-30T14:46:19Z")

</div>

On 30 Oct 2008, at 13:35, Raphael B. wrote:

> Hi,
> 
> I have an Instance model in my application, to which I want to add a  
> has\_many :web\_file\_attachment from the plugin.  
> I tried putting this in my plugin’s app/model/instance.rb :  
> class Instance \< ActiveRecord::Base  
> has\_many :web\_file\_attachments , :dependent =\> :destroy  
> end
> 
> but it doesn’t seem to work.

The engines plugin doesn’t mix model code (yet), so it’s unlikely that  
your plugin model is being loaded.

A better way to do this would be to add a ‘macro’ method to  
ActiveRecord::Base, which you can then call in your main application  
model, along the lines of

app/models/instance.rb

class Instance \< ActiveRecord::Base  
# other stuff…  
has\_web\_file\_attachments  
end

This would give you more flexibility, I think.

The main reason why your current setup doesn’t work, is that the  
engines plugin isn’t loading your plugin model.

James

---

<div class="post-metadata">

**Author:** ![raphael\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@raphael\_b](https://www.ruby-forum.com/u/raphael_b)\
**Post date:** [October 30, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/adding-belongs-to-to-an-application-model-from-an-engine/152142/3 "2008-10-30T14:57:27Z")

</div>

On Thu, Oct 30, 2008 at 2:45 PM, James A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> class Instance \< ActiveRecord::Base  
> # other stuff…  
> has\_web\_file\_attachments  
> end
> 
> This would give you more flexibility, I think.

Yes, but I’m looking at engines to be able to add this without  
touching the main app.  
Actually I’m building a new datatype for [dedomenon.org](http://dedomenon.org) , and it would  
be great if it was possible without touching the main application’s  
code: just install the engine and you’re done.  
Isn’t there a way to achieve this?

Thanks for your help!

Raph

---

<div class="post-metadata">

**Author:** ![raphael\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@raphael\_b](https://www.ruby-forum.com/u/raphael_b)\
**Post date:** [October 30, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/adding-belongs-to-to-an-application-model-from-an-engine/152142/4 "2008-10-30T15:00:23Z")

</div>

On 30 Oct 2008, at 13:56, Raphael B. wrote:

> Yes, but I’m looking at engines to be able to add this without  
> touching the main app.  
> Actually I’m building a new datatype for [dedomenon.org](http://dedomenon.org) , and it would  
> be great if it was possible without touching the main application’s  
> code: just install the engine and you’re done.  
> Isn’t there a way to achieve this?
> 
> Thanks for your help!

In this case, you’ll need to ensure that the main application  
instance.rb model is loaded before you reopen the class in your  
plugin. I would probably do something along the lines of

my\_plugin/init.rb

require ‘instance\_extensions’

my\_plugin/lib/instance\_extensions.rb

require ‘instance’ # maybe raise an error here if it’s missing in  
the main app  
Instance.class\_eval do  
has\_many :web\_file\_attachments # …  
end

Hope that helps,

James

---

<div class="post-metadata">

**Author:** ![raphael\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@raphael\_b](https://www.ruby-forum.com/u/raphael_b)\
**Post date:** [October 31, 2008, 10:33am UTC](https://www.ruby-forum.com/t/adding-belongs-to-to-an-application-model-from-an-engine/152142/5 "2008-10-31T10:33:47Z")

</div>

On Thu, Oct 30, 2008 at 2:59 PM, James A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘instance’ # maybe raise an error here if it’s missing in the main  
> app  
> Instance.class\_eval do  
> has\_many :web\_file\_attachments # …  
> end
> 
> Hope that helps,

It’s going in the right direction, but it’s not working yet as I get  
this error when starting the application:

=\> Booting WEBrick…  
/home/rb/dev/dedomenon/vendor/rails/activerecord/lib/active\_record/associations.rb:1273:in  
`before_destroy': wrong number of arguments (1 for 0) (ArgumentError) from /home/rb/dev/dedomenon/vendor/rails/activerecord/lib/active_record/associations.rb:1273:in `configure\_dependency\_for\_has\_many’  
from  
/home/rb/dev/dedomenon/vendor/rails/activerecord/lib/active\_record/associations.rb:711:in  
`has_many' from /home/rb/dev/dedomenon/vendor/plugins/web_file_attachment/lib/instance_extensions.rb:3 from /home/rb/dev/dedomenon/vendor/plugins/web_file_attachment/lib/instance_extensions.rb:2:in `class\_eval’  
from  
/home/rb/dev/dedomenon/vendor/plugins/web\_file\_attachment/lib/instance\_extensions.rb:2  
from  
/usr/local/lib/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’  
from  
/home/rb/dev/dedomenon/vendor/rails/activesupport/lib/active\_support/dependencies.rb:509:in  
`require' ... 26 levels... from /home/rb/dev/dedomenon/vendor/rails/activesupport/lib/active_support/dependencies.rb:509:in `require’  
from  
/home/rb/dev/dedomenon/vendor/rails/railties/lib/commands/server.rb:39  
from ./script/server:3:in `require’  
from ./script/server:3

The line causing the problem is  
has\_many :web\_file\_attachments , :dependent =\> :destroy  
This exact same line put in the Instance class of the application works  
fine.

Any hints?

Thanks!

Raph
