I want to limit my association to two (one of every scope). I tried:
has_many :associations
has_many :associations_with_first_scope, :class_name => 'Association', :conditions => {...}
has_many :associations_with_second_scope, :class_name => 'Association', :conditions => {...}
validates :associations, {:maximum => 4}
validates :associations_with_first_scope, {:maximum => 2}
validates :associations_with_second_scope, {:maximum => 2}
and I also tried custom validator (i tried count, size and length also):
validate :custom_associations_limit
def custom_associations_limit
error.add(:base, '...') if associations_with_first_scope.size > 2
error.add(:base, '...') if associations_with_second_scope.size > 2
end
I also tried to put validation into association model. Nothing works. I think my problem caused by using nested_form gem. When I have form with four associations (two of each kind) and I'll delete tow and add tow, model thinks it has six associations instead of four. Because it is validating probably before nested_attributes are passed (with allow_destroy set to true).
Any can help with this?