作者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/m.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