Allows you to specify the number of has_many relations in ActiveRecord association declarations.
-
Install the plugin/gem
-
Define a template relation instead of a has_many
class User < ActiveRecord::Base template_relation :images, :number => 3 end --> A user now must exactly have 3 images or it will not validate user = User.new user.image_1 = Image.new(:name => 'Pic 1') user.image_2 = Image.new(:name => 'Pic 2') user.image_3 = Image.new(:name => 'Pic 3') user.save! user.update_attributes(:image_1 => {:name => 'Nice Image'}) assert_equal 'Pic 3', user.image_3.name assert_equal 'Nice Image', user.image_1.name