I have the following code for a plugin that I am developing:
module ClassMethods def my_method(child_table)
child = child_table.to_s; c = child.pluralize
has_and_belongs_to_many c.to_sym, :class_name => self.name, :join_table => c.to_s + '_' + self.name.pluralize, :association_foreign_key => child + "_id", :foreign_key => self.name + "_id", :after_add => :create_bidirectional_link, :after_remove => :remove_bidirectional_link
class_eval do
define_method("create_bidirectional_link(child)") do child.c << self unless child.c.include?(self) end
define_method("remove_bidirectional_link(child)") do child.c.delete(self) rescue nil end end
end
When I run the code I get the following error message when I use my_method in the Person model class:
NoMethodError: undefined method `create_bidirectional_link' for #<Person:0x31dd71c>
How do I inject the create and remove methods to the ActiveRecord class? TIA.