这是我的模型的精简版。
model Paper
PAPER_STARTING_NUMBER = 1
validate_uniqueness_of :number, :allow_blank => true
before_create :alocate_paper_number
def alocate_paper_number
return true if self.number.present?
p_number = Paper.maximum('number') || Paper::PAPER_STARTING_NUMBER
self.number = p_number >= Paper::PAPER_STARTING_NUMBER ? p_number+1 : Paper::PAPER_STARTING_NUMBER
return true
end
end
问题是我在数字列中有重复项。任何想法为什么以及如何在不更改回调的情况下解决此问题。我知道我可以在数据库上添加唯一性验证或对该列进行序列化,还有其他想法吗?