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.
我有一个 ActiveRecord 模型,它需要有一个辅助 id。我可以想到几种方法来实现它,但最好的方法是什么?是否可以在迁移中指定自动增量,还是应该在过滤器中完成?
最佳做法是什么?
谢谢。
这是一个解决方案
before_validation :add_secondary_id, :on => :create def add_secondary_id self.secondary_id = self.class.select(:secondary_id).order('secondary_id DESC').first.secondary_id.to_i + 1 end