在铁路3.2.3中,我要证实,连接模式具有两个领域的独特结合。 我有检验和验证办法,通过如下所示测试,但似乎有更好的办法这样做。 例如,使用独一无二的指数是否更好? 如果是,为什么?
# link_test.rb
...
test "cannot create two links with same name and url" do
Link.create!(:name => example , :url => http://www.example.com )
assert_raise(ActiveRecord::RecordInvalid, could create two links with same name and url ){Link.create!(:name => example , :url => http://www.example.com )}
end
...
# link.rb
class Link < ActiveRecord::Base
...
validates :name, :uniqueness => {:scope => :url, :message => cannot have two entries with same name and url }
...
end