作者godfat (godfat 真常)
看板Ruby
标题Re: [问题] 如何以DRY的方式写code?
时间Wed Oct 7 15:54:21 2009
※ 引述《bypang (甚麽时候才等到你)》之铭言:
: 环境:
: Ruby 1.8.7 Rails 2.3.4
: 状况:
: 我写的是一个企业管理程式,在每开一个新的作业流程都会先取得一个新的编号
: (不是预设的id)。例如Quotation,我在Quotations Helper里是这样写:
: module QuotationsHelper
: def new_doc_no
: Quotation.find(:first, :order => 'doc_no DESC').doc_no + 1;
: end
: end
: 至于其他的作业流程是否也要写类似的helper function,还是有更DRY的方式?
: 谢谢指教
不介意的话可以这样试试看 @@ 前提当然是可以改 primary key 的话
class Quotation < ActiveRecord::Base
set_primary_key :doc_no
end
module QuotationsHelper
def new_doc_no
Quotation.last.doc_no + 1
end
end
--
#!/usr/bin/env ruby [露比] /Programming (Kn|N)ight/ 看板《Ruby》
# if a
dog nailed
extra legs that
http://webptt.com/cn.aspx?n=bbs/Ruby/index.html
#
walks like an octopus, and Welcome ~
Ruby@ptt~
#
talks like an octopus, then
◢█◣ http://www.ruby-lang.org/
# we are happy to treat it as
█ http://www.ruby-doc.org/
# if it were
an octopus.
◥ ◤ http://www.rubyforge.org/
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.128.121.85