【问题标题】:Searchkick filter with scope带范围的 Searchkick 过滤器
【发布时间】:2016-04-27 04:39:26
【问题描述】:

有人使用 Searchkick 和作用域模型吗?

我有一个带有scope :published, ->{ where(status: "Published") }Article 模型。 所以在我的ArticleController 中,我做了以下事情: query = params[:q].presence || "*" @search = Article.published.search(query, operator: "or", suggest: true)

但它不起作用 https://github.com/ankane/searchkick/issues/140 并保留包括所有记录

所以我做了: query = params[:q].presence || "*" @search = Article.search(query, where:{status: "Published"}, operator: "or", suggest: true)

也没有工作......并且没有返回任何内容。 不知道我做错了什么。

【问题讨论】:

  • 那个问题 #140 似乎还没有解决。想知道为什么带有where 的第二个不起作用,您确定是Published 而不是published

标签: ruby-on-rails scope searchkick


【解决方案1】:

我只需要将status 添加到search_data 哈希中

class Article < ActiveRecord::Base

  searchkick highlight: [:title], text_start: [:title], language: "spanish"

  scope :draft,     ->{ where(status: "Draft") }
  scope :published, ->{ where(status: "Published") }
  scope :scheduled, ->{ where(status: "Scheduled") }

  def search_data
    {
      title: title,
      content: content,
      status: status     <--------
    }
  end

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多