views:

6

answers:

0

hi all

I have a has_many_polymorphs relationship that involves the following classes:

class DigilearningModule < ActiveRecord::Base
  has_many_polymorphs :members,
   :from => [:digilearning_titles, :lesson_categories,
:lesson_category_groups],
   :through => :digilearning_module_memberships,
   :dependent => :destroy,
   :uniq => true
end

class DigilearningModuleMembership < ActiveRecord::Base
  belongs_to :digilearning_module
  belongs_to :member, :polymorphic => true
  acts_as_list :scope => :digilearning_module
end

The classes listed in the from option have no further associations with DigilearningModule or DigilearningModuleMembership, besides the ones dynamically added by has_many_polymorphs.

As you can see, :uniq is set. This uniqueness is enforced on the has_many relationship created from DigilearningModule to (eg) DigilearningTitle, ie if i add a digilearning_title to a digilearning_module multiple times, then the digilearning_module still only has one digilearning_title. This is fine.

However, the uniqueness isn't enforced in the opposite direction. I can add a digilearning_module to the same digilearning_title multiple times.

Before i go and start trying to monkey patch the has_many_polymorphs code to add this uniqueness to the dynamically created has_many associations it adds to its target classes, can anyone tell me a) if this is normal b) is there an easy fix?

thanks - max