I have two models
class Group < AR
has_many :permissions
accepts_nested_attributes_for :permissions, :allow_destroy => true
end
class Permission < AR
validates_uniqueness_of :action, :scope => [:role]
end
I can't seem to get the unique constraint on permissions to work when creating a new group, only on update. Here's a sample output. Does anyone know the best way to get validation to work on nested attributes and unique constraints?
sample output
> g = Group.create(:permissions_attributes => [{:role => 'admin', :action => 'one'}])
> # Now add the same permissions, should not be valid
> g.permissions_attributes = [{:role => 'admin', :action => 'one'}]
> g.valid? # => false
That is expected. However if I create the Group with the same permissions_attributes twice, it doesn't invalidate:
> g = Group.new(:permissions_attributes => [{:role => 'admin', :action => 'one'}, {:role => 'admin', :action => 'one'}]
> g.valid? # => true BUT THIS SHOULD BE FALSE!!
> g.save # => true Oh Nos!!!