使用链接时,ActiveRecord不会过滤

时间:2013-03-24 19:20:17

标签: ruby-on-rails activerecord

  # Get only non-empty reviews
  query = Review.where("content <> ''")

  # filter on status (unreviewed, reviewed, flagged, etc), if provided
  if (status)
    query.where("status = ?", status)
  end

  # Order rows
  query.order("created_on ASC")

  # iterate, this should fire the query
  reviews = query.each_with_object([]) do |review, a|

我的问题是我的查询没有过滤'状态'。我加倍检查我的代码是否在if语句中。我甚至走了:

  # Get only non-empty reviews
  query = Review.where("content <> ''")

  # filter on status (unreviewed, reviewed, flagged, etc), if provided
  query.where("status = ?", status)

我确信我做错了但我不知道

1 个答案:

答案 0 :(得分:1)

刚刚找到它。需要每次都分配查询

  # Get only non-empty reviews
  query = Review.where("content <> ''")

  # filter on status (unreviewed, reviewed, flagged, etc), if provided
  query.where("status = ?", status)