English 中文(简体)
利用铁路面积确认两个领域的独特性的最佳途径
原标题:Best way to validate uniqueness of two fields together using scope in rails

在铁路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
最佳回答

我将使用这两条。

使用独一无二的指数的好处是,数据库也将加以执行。 例如,如果你拥有半个数十个应用服务器,其中每个服务器都可能产生新的记录,那么保护铁路独一无二之处就很难低估这一问题。 缺点是,要从中恢复这一点就更加困难。

在积极记录中这样做的好处是,你可以很容易地加以 de弄,获得干净的例外信息,并更有效地向用户传达这一问题。 缺点是,它需要额外的询问,而且更容易受到多读/多处理类型错误的影响。

如果可能的话,只要与数据库独有指数的绝对可靠性相结合,你就能够收回积极记录。 你在很短的时间里仍然可以看到一幅不同的错误,当时有两项要求同时遭到打击,但你将保持数据与你的制约因素一致。

问题回答

暂无回答




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签