开发者

Proper way to validate uniqueness_of for multiple columns

Let's say I have this unique index on a table:

add_index :events, [:venue_id, :act_id, :occurs_on], :unique => true

Now if I want to validate this in the model, should I do this:

validates_uniqueness_of :venue_id, :scope => [:act_id, :occurs_on]

or this:

validates_uniqueness_of :venue_id, :scope => [:act_id, :occurs_on]
valid开发者_StackOverflow中文版ates_uniqueness_of :act_id, :scope => [:venue_id, :occurs_on]
validates_uniqueness_of :occurs_on, :scope => [:venue_id, :act_id]


Your first uniqueness constraint translates to "There can only be one of any particular venue_id, act_id, and occurs_on tuple in the table." Therefore only the first validation statement should be necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜