Question

In rails 3.2.3, I want to validate that a link model has a unique combination of two fields. I have a test and a validation that passes the test as shown below, but it seems there may be a better way to do this. For instance, would it be better to use an index with uniqueness? If so, why?

# 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
Était-ce utile?

La solution

I would use both.

The advantages to using a unique index is that the database will also enforce it. If, for instance, you have a half a dozen application servers each one of which might create a new record then protecting uniqueness in rails is extremely difficult to understate the problem. The disadvantage is that this is harder to recover from.

The advantages to doing it in in active record is that you can easily debug it, get clean exception messages and communicate the problem to the user more effectively. The disadvantage is that it requires an extra query and it is more vulnerable to multithread/multiprocess type errors.

With both you get the recoverability of an active record validation when it is possible combined with the absolute reliability of a database unique index. You can still see a different error in the rare instances when two requests hit at the exact same moment, but it will keep your data inline with your constraints.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top