Adding belongs_to to an application model from an engine

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

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

On Thu, Oct 30, 2008 at 2:45 PM, James A. [email protected] 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 , 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

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 , 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

On Thu, Oct 30, 2008 at 2:59 PM, James A. [email protected] 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