Ruby on rails 未定义方法downcase

Ruby on rails 未定义方法downcase,ruby-on-rails,Ruby On Rails,我正在努力使我的搜索方法适用于Heoku。我以前有过这个问题,加上。downcase解决了它。 但是现在,它不起作用了,我 nil:NilClass的未定义方法“downcase” listing.rb def self.locsearch(search_location, search_description) return scroped unless search_location.present? && search_description.present? wh

我正在努力使我的搜索方法适用于Heoku。我以前有过这个问题,加上。downcase解决了它。 但是现在,它不起作用了,我 nil:NilClass的未定义方法“downcase”

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
现在它返回所有列表,即使我输入的位置和描述与某个列表匹配。
为什么?

搜索位置或
搜索描述
的值是
nil
,因此当您尝试调用其
downcase
方法时,没有。因此出现了错误


顺便说一句,您可能指的是
范围
而不是
范围
搜索位置
搜索描述
的值是
nil
,因此当您尝试调用其
downcase
方法时,没有。因此出现了错误


顺便说一句,您可能指的是
范围
而不是
范围
搜索位置
搜索描述
的值是
nil
,因此当您尝试调用其
downcase
方法时,没有。因此出现了错误


顺便说一句,您可能指的是
范围
而不是
范围
搜索位置
搜索描述
的值是
nil
,因此当您尝试调用其
downcase
方法时,没有。因此出现了错误


顺便说一下,您可能是指
范围
而不是
搜索

仅当搜索位置和搜索描述都存在时才返回搜索

如果其中只有一个是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

这样更容易发现错误。

只有在搜索位置和搜索描述都存在的情况下,才返回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

这样更容易发现错误。

只有在搜索位置和搜索描述都存在的情况下,才返回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

这样更容易发现错误。

只有在搜索位置和搜索描述都存在的情况下,才返回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

这样更容易发现错误。

谢谢马克,这很有效。我用self.all替换了scroped(这本来是用来确定范围的,但我用错了)。谢谢马克,这很有效。我用self.all替换了scroped(这本来是用来确定范围的,但我用错了)。谢谢马克,这很有效。我用self.all替换了scroped(这本来是用来确定范围的,但我用错了)。谢谢马克,这很有效。我用self.all替换了scroped(这本来是用来确定范围的,但我用错了)。