ruby 代码
- class Enterprise < ActiveRecord::Base
- # 当创建前调用auto_id 方法:
- before_create :auto_id # auto_id通过调用empno_next方法设置name
- def auto_id
- self.name = empno_next
- end
- # 通过重数据库中读出最大的ID
- # 但是如果是复合ID?下次解决。。(思路。。增加字段)
- def empno_next
- num = ActiveRecord::Base.connection.select_value('select max(name)+1 from enterprises')
- if num.nil? # 判断空情况。。初始化。。
- num = 10000
- return num
- end
- return num
- end
- end