I’m looking to write a test that can validate the uniqueness of a compound validation (if that makes sense). The validation itself is as follows:
validates_uniqueness_of :name, :scope => [:address_postcode]
How do I do that? Would I simply try and create two places with the same name and try to validate? For example (and this code doesn’t work)
test "name and postcode of a new place must be unique" do
place = Place.new(description: "my favourite chinese", cuisine:"chinese",
address_state:"NSW", address_country:"Australia", address_postcode:"2209")
place.name = "Masthai"
assert place.valid?
place2 = Place.new(description: "try a second restaurant", cuisine:"chinese",
address_state:"NSW", address_country:"Australia", address_postcode:"2209")
place2.name = "Masthai"
assert place2.invalid?
end
place2is probably valid because you’re not savingplaceto the database. I would try this:Also, I’d like to offer a suggestion on formatting/style:
Also, I’d recommend that you read a little bit about database normalization. You should really have separate tables for
cuisine,stateandcountry, rather than storing these values as freeform text.