我正在尝试让我的搜索方法适用于Heoku。我以前遇到过这个问题,然后添加.downcase来修复它。 但现在,它不起作用,我得到了 nil的未定义方法`downcase':NilClass
listing.rb
def self.locsearch(search_location, search_description)
return scroped unless search_location.present? && search_description.present?
where(["LOWER(location) LIKE? AND LOWER(description) LIKE?", "%#{search_location.downcase}%", "%#{search_description.downcase}%"])
end
有谁知道问题是什么?
我把它改成了
def self.locsearch(location, description)
if location.present? && description.present?
where(["LOWER(location) LIKE? AND LOWER(description) LIKE?", "%#{location.downcase}%", "%#{description.downcase}%"])
else
self.all
end
end
现在它返回所有列表,即使我的位置和描述输入与某个列表匹配。 为什么呢?
答案 0 :(得分:1)
search_location
或search_description
的值为nil
,因此当您尝试调用其downcase
方法时,没有。因此错误。
顺便说一句,您可能指的是scoped
而不是scroped
。
答案 1 :(得分:1)
只有当search_location和search_description都存在时,才会返回scroped。
如果只有一个是nil,则执行下一行,并在nil上调用downcase。
我会像这样重写你的代码:
self.search_location(location, description)
if location.present? && description.present?
where(["LOWER(location) LIKE? AND LOWER(description) LIKE?",
"%#{location.downcase}%", "%#{description.downcase}%"])
else
scroped #not sure what it is, maybe you need another name for this?
end
end
这样可以更容易发现错误。