0

是的,有一个几乎完全一样的问题,但我认为它没有以一种轨道的方式得到回答。它是如此不干燥,它伤害 了 Rails 模型中不区分大小写的搜索

我想在我的 postgresql 数据库中搜索基于名称的产品。我也可能想在同一个数据库中搜索客户。

所以我利用了上一个问题中的建议。我的产品表 Product.find_by_name #takes the name and finds 正如前面的答案所暗示的那样,我在我的产品表中有一个函数。

轻松如!现在客户怎么...

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 个月的人,可以指出一个真正的 DRY。

4

1 回答 1

1

您是否尝试同时搜索所有这些属性?如果是这样,您可能想尝试使用像Thinking Sphinx这样的搜索引擎

于 2012-06-25T00:45:28.367 回答