Re: [問題] 如何以DRY的方式寫code?

看板Ruby作者 (godfat 真常)時間15年前 (2009/10/07 15:54), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/4 (看更多)
※ 引述《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://www.ptt.cc/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
文章代碼(AID): #1Ap4amEi (Ruby)
討論串 (同標題文章)
文章代碼(AID): #1Ap4amEi (Ruby)