我之前問過一個問題,其中引出了一些很好的回答。將業務規則移入模型
上給出有一些建議的背影,我已經試過以下控制器邏輯
if params[:concept][:consulted_legal] == 0 && params[:concept][:consulted_marketing] == 1
@concept.attributes = {:status => 'Awaiting Compliance Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 1
@concept.attributes = {:status => 'Awaiting Marketing Approval'}
elsif params[:concept][:consulted_marketing] == 0 && params[:concept][:consulted_legal] == 0
@concept.attributes = {:status => 'Awaiting Marketing & Legal Approval'}
else
@concept.attributes = {:status => 'Pending Approval'}
end
移動到模型中,像這樣:
def set_status
if status.blank?
if (consulted_legal == true) && (consulted_marketing == true)
status = "Pending Approval"
elsif (consulted_legal == true) && (consulted_marketing == false)
status = "Awaiting Marketing Approval"
elsif (consulted_legal == false) && (consulted_marketing == true)
status = "Awaiting Legal Approval"
elsif (consulted_legal == false) && (consulted_marketing == false)
status = "Awaiting Marketing & Legal Approval"
end
end
true # Needs to return true for the update to go through
end
我從before_save回調調用。
作爲默認情況下,該consulted_legal和consulted_marketing屬性設置爲false和not null,這就是爲什麼我測試==或真或假這裏,而不是問
if consulted_legal?
例如。
但是,這種邏輯似乎沒有工作。如果我檢查對象,那麼狀態不會被設置爲任何東西。任何人都可以發現爲什麼這可能會發生?例如,我有模型中的屬性訪問錯誤嗎?
TIA
我懷疑他有那些專欄,那是他們來自控制器。確實是 – Samuel 2009-01-23 16:32:24