Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个用户数据库,它有 Company 列,它可以是空的,即 null 或者可能包含一些值。但如果包含任何值,它应该是唯一的。如果我在模型中使用唯一属性,则不允许该列有多个空值。我正在使用 Sqlite3 数据库。
您可以使用模型验证来处理它。也许是这样的:
class User < ActiveRecord::Base attr_accessible :company validate do if self.company && User.where(company: self.company).first raise ArgumentError, "Company must be `nil` or unique" end end end
这有点小技巧,但它应该满足您的需求。