2012-06-01 36 views
0

是的,有一個問題幾乎完全像這樣,但我不認爲它是以rails的方式回答的。它是如此不幹,它傷害Case-insensitive search in Rails modelRails模型中不區分大小寫的搜索Rewrite

我想搜索我的postgresql數據庫的基於名稱的產品。 我也可能想爲客戶搜索相同的數據庫。

所以我利用了上一個問題中的建議。 我在我的產品表中有一個功能 Product.find_by_name#出現這個名字,並且與前面的答案一樣。

簡單!現在客戶如何...

Customer.find_by_contact_name 
Customer.find_by_email_address 
Customer.find_by_company_name 
Customer.find_by_phone 
Customer.find_by_contact_name_and_phone 
Customer.find_by_industry_id_and_contact_name #the insdustry_id is not text so it can't be found. 
# Ahh snot I forgot a few. 
Customer.find_by_contact_name_and_company_name 
Customer.find_by_contact_name_and_email_address 
Customer.find_by_contact_name_and_phone 
Customer.find_by_industry_id_and_email_address 
... 

這些都必須硬編碼。當我需要另一個時,我必須打開客戶,複製代碼並創建一個新函數。因爲我不知道值是整數還是字符串或日期。

我正在尋找一種方法,不必爲每個模型和每次迭代搜索實現此操作,我可能會在將來的某個時間點執行此操作。

我想對另一個問題的答案表示感謝。他們看起來並不像「Rails」那樣,而且我希望有更多的東西。手指交叉,有4個月以上的人,可以指向我一個真正的幹。

+0

你指的是哪個問題? (可能有助於w /上下文) –

+0

添加鏈接到其他「相同」的問題 – baash05

+0

可能要結帳 - https://github.com/ernie/ransack - 並通過其中一些看看? http://railscasts.com/?tag_id=30 - 如果你自己手上角色,我傾向於使用AREL,其語法爲'Customer.where(「company_name ILIKE?」,params [:company_name])if params [:company_name] .present?' – house9

回答

1

您是否試圖同時搜索所有這些屬性?如果是這樣,你可能想嘗試使用搜索引擎,如Thinking Sphinx