0

我试图创造我的搜索条件,但我遇到了一些麻烦。

这是我正在尝试创建的方法。

def self.searchadv(title, place, category, date)
    !title.blank? ? conditions = ['title LIKE ?', "%#{title}%"] : conditions = []
    if conditions
        !place.blank? ? conditions << [' AND place LIKE ?', "%#{place}%"] :  conditions << []
        !place.blank? ? conditions << [' AND category LIKE ?', "%#{place}%"] :  conditions << []
        !place.blank? ? conditions << [' AND date LIKE ?', "%#{place}%"] :  conditions << []
    else 
        !place.blank? ? conditions << [' place LIKE ?', "%#{place}%"] :  conditions << []
        !place.blank? ? conditions << [' category LIKE ?', "%#{place}%"] :  conditions << []
        !place.blank? ? conditions << [' date LIKE ?', "%#{place}%"] :  conditions << []
    end
    find(:all, :conditions => conditions)
end

它有效,直到我尝试附加 place 参数并收到此错误

错误数量的绑定变量(4 为 1)在:title LIKE ?

如果我删除这个:

if conditions
    !place.blank? ? conditions << [' AND place LIKE ?', "%#{place}%"] :  conditions << []
    !place.blank? ? conditions << [' AND category LIKE ?', "%#{place}%"] :  conditions << []
    !place.blank? ? conditions << [' AND date LIKE ?', "%#{place}%"] :  conditions << []
else 
    !place.blank? ? conditions << [' place LIKE ?', "%#{place}%"] :  conditions << []
    !place.blank? ? conditions << [' category LIKE ?', "%#{place}%"] :  conditions << []
    !place.blank? ? conditions << [' date LIKE ?', "%#{place}%"] :  conditions << []
end

一切都很好,但我需要其他选项来创建我的搜索,我不明白为什么错误出现在“LiKE”中

有人可以帮助我吗?

提前致谢!

4

2 回答 2

0

这非常难看,难以阅读/调试,因此容易出错

def self.searchadv(title, place, category, date)
  conditions              = {}
  conditions[:title]      = "%#{title}%" if title.present?

  if place.present?
    conditions[:place]    = "%#{place}%"
    conditions[:category] = "%#{category}%"
    conditions[:date]     = "%#{date}%"
  end

  where(conditions)
end

编辑正如 OP 指出的那样,上面不允许通配符匹配。下面使用ARel的matches方法来完成这个。

def self.searchadv(title, place, category, date)
  offers     = self.arel_table
  predicates = []

  predicates << offers[:title].matches("%#{title}%") if title.present?

  if place.present?
    predicates << offers[:place].matches("%#{place}%")
    predicates << offers[:category].matches("%#{category}%")
    predicates << offers[:date].matches("%#{date}%")
  end

  if predicates.size > 1
    first = predicates.shift
    conditions = Arel::Nodes::Grouping.new(predicates.inject(first) {|memo, expr| Arel::Nodes::And.new(memo, expr)})
  else
    conditions = predicates.first
  end

  where(conditions).to_a
end
于 2012-12-02T22:44:43.383 回答
0

我首先尝试保留您的代码并且只尝试更正它,但是有很多不好的事情:) 这就是我将如何做到它保持与您使用的相同的结构(但也许最好使用条件哈希正如@Deefour 的建议):

def self.searchadv(title, place, category, date)
  cond_ary = []
  cond_values = []
  unless title.blank?
    cond_ary << 'title LIKE ?'
    cond_values << "%#{title}%"
  end
  unless place.blank?
    cond_ary << 'place LIKE ? AND category LIKE ? AND date LIKE ?'
    cond_values.push("%#{place}%", "%#{place}%", "%#{place}%")
  end
  conditions = [ cond_ary.join(' AND ') ] + cond_values
  find(:all, :conditions => conditions)
end

我可以建议您研究条件数组的外观,然后在 ruby​​ 控制台中使用数组。例如ary << 1,用ary << [1,2,3], ary.concat([1,2,3]),ary.push(1,2,3)等查看数组会发生什么。

当你这样做时

expr ? x = 1 : x = 2

最好用

x = expr ? 1 : 2
于 2012-12-03T00:12:19.480 回答