Forum: Ruby on Rails Factoring code out of a model but not plugin

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.
petermichaux (Guest)
on 2005-11-21 01:16
(Received via mailing list)

I have been trying for hours to figure out how to factor the following
out of my Product model so that I can share it with other models. Since
method_missing method is dependent on another model a plugin would not
stand alone. I've tried to create a module and require, include, extend
Product but nothing I have tried works. Any suggestions?


require 'free_form.rb'

class Product < ActiveRecord::Base
has_and_belongs_to_many :categories

belongs_to :manufacturer

has_many :variations

has_many :images,
:order => :position,
:foreign_key => 'owner_id',
:conditions => "owner_class = '#{self.to_s}'"

# the following two has_and_belongs_to_many statements
# create a self-referential many-to-many relationship for cross sell
has_and_belongs_to_many :cross_sell_products,
:join_table => 'cross_sells',
:foreign_key => 'product_id',
:association_foreign_key => 'cross_sell_product_id',
:class_name => 'Product',
:order => 'cs_position'

has_and_belongs_to_many :cross_seller_products,
:join_table => 'cross_sells',
:foreign_key => 'cross_sell_product_id',
:association_foreign_key => 'product_id',
:class_name => 'Product'

# :TRICKY: pm : The next statement works because only distinguishing
# are linked in the join table.
# That is, the nondistinguising virtual attributes are not 'seen'
the join table.
has_and_belongs_to_many :distinguishing_virtual_attributes,
:class_name => 'VirtualAttribute'

has_and_belongs_to_many :optional_variation_groups

# stuff below is for the free_form table concept
# turns out it is very intertwined in the app and probably
# should not be a plugin
# however I would like to factor out the code below so that
# it can be mixed in to any model

# module_eval do
# extend FreeForm
# end
# require 'free_form.rb'

# extend FreeForm

#module_eval do
# has_many :virtual_attribute_values,
# :foreign_key => 'owner_id',
# :conditions => "owner_class = '#{self.to_s}'",
# :dependent => true

has_many :virtual_attribute_values,
:foreign_key => 'owner_id',
:conditions => "owner_class = '#{self.to_s}'",
:dependent => true

def method_missing(method_id, *args, &block)

# Try to execute the message with all superclass or module
rescue NoMethodError

end # rescue NoMethodError
end # method_missing

end # class Product
petermichaux (Guest)
on 2005-11-21 01:24
(Received via mailing list)
Sorry for the double post. Please ignore this tread
This topic is locked and can not be replied to.