我正在製作一個簡單的rails站點,它將存儲一些日期並執行基本的條件檢查。我在下面寫了幾個方法,並被告知我可以使它們更有效率。我一直在抓我的頭,我不知道如何做到這一點。我應該使entry.find全球?還是有更明顯的解決方案?在此先感謝Ruby重構
def name
@fname = params[:fst_name]
@lname = params[:lst_name]
@entry = Entry.create({:first_name => @fname, :last_name => @lname})
end
def attribs
@person = Entry.find(:last)
@fname = @person.first_name
@lname = @person.last_name
@person.update_attributes({:address => params[:st_name],
:salary => params[:salary], :loan => params[:loan],
:loan_reason => params[:reason]})
if [email protected]? then render "show" end
end
def show
@person = Entry.find(:last)
end
def modify
@person = Entry.find(:last)
@fname = @person.first_name
@lname = @person.last_name
@entry = Entry.create({:first_name => @fname, :last_name => @lname,
:salary => params[:salary], :loan => params[:loan]})
end
def borrow
@person = Entry.find(:last)
if [email protected]? then
if (@person.salary * 3) < @person.loan
then @message = "You have asked for too much"
else @message = "No problem"
end
else @message = "empty record?"
end
end
end
東西這類問題也許應該對http://codereview.stackexchange.com要求 –