1
我在表單中動態地填充選擇標記。選擇標記通過查詢填充到Payee
。我有一些字段想顯示爲元素的文本值。但是它必須有條件地完成。例如。Rails有條件的選擇內容
領域:
:company
:first_name
:last_name
我可以連接在文本框中輸入這三個像這樣:
f.select :payee_id, Payee.all.collect { |p| [p.company.to_s + " " + p.first_name.to_s + " " + p.last_name.to_s, p.id] }
以上不工作我想要的方式。如果company
存在,我想省略first_name
和last_name
。
如果company
是空白的,我想只顯示first_name
和last_name
目前如果company
和first_name
存在它加到兩個在一起,我不想要。
我知道如何分別查詢每個案例的數據庫,但我不知道如何組合結果,因此select標籤將正確顯示。
到目前爲止,以下是我的幫手。我知道它現在是一團糟,但我需要幫助理解完成這件事的方法。
def expense_payee_id_field(f)
@payee_with_company = Payee.where("company IS NOT NULL and company !='' ")
@payee_without_company = Payee.where("(first_name != '' or last_name != '') and company = '' ")
f.select :payee_id, Payee.all.collect { |p| [p.company.to_s + " " + p.first_name.to_s + " " + p.last_name.to_s, p.id] }
end
簡單。只有一行邏輯大聲笑 – ctilley79
得愛紅寶石! –
最後一個問題。這適用於NULL值,但不適用於空字符串。什麼情況下會測試空字符串以及NULL?應該空字段爲NULL? – ctilley79