- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 Controller 索引中的代码。对于@posts,我只显示已发布的帖子和用户不喜欢的帖子,rsvp等。当我添加对searchkick的搜索时,它将忽略其他条件。搜索有效,但其中包含未发布的帖子等。如何在模型或 Controller 中进行调整以能够搜索和限制我的查询。
@favorites = current_user.favorites
@rsvps = current_user.rsvps
@unshows = current_user.unshows
query = params[:q].presence || "*"
@posts = Post.published.where.not(id: @unshows).where.not(id: @favorites).where.not(id: @rsvps).search(query)
class Post < ActiveRecord::Base
belongs_to :user
default_scope -> { order(created_at: :desc) }
searchkick text_start: [:title]
def search_data
{
title: title,
description: description,
location: location,
date: date
}
end
validates :user_id, presence: true
validates_presence_of :slug
has_many :repluggers, :class_name => 'user', :foreign_key => 'user_id'
has_many :replugs, :class_name => 'post', :foreign_key => 'replug_id'
has_attached_file :image, :styles => { :medium => "800x800#", :thumb => "100x100#" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
# Favorited by users
has_many :favorite_posts # just the 'relationships'
has_many :favorited_by, through: :favorite_posts, source: :user
# Favorited by users
has_many :rsvp_posts # just the 'relationships'
has_many :rsvp_by, through: :rsvp_posts, source: :user
# Favorited by users
has_many :unshow_posts # just the 'relationships'
has_many :unshow_by, through: :unshow_posts, source: :user
scope :draft, ->{where(published_at: nil)}
scope :published, ->{where.not(published_at: nil).where("published_at <= ?", Time.now.in_time_zone("Eastern Time (US & Canada)"))}
scope :scheduled, ->{where.not(published_at: nil).where("published_at >= ?", Time.now.in_time_zone("Eastern Time (US & Canada)"))}
attr_accessor :status
before_validation :clean_up_status
def clean_up_status
self.published_at = case status
when "Draft"
nil
when "Published"
Time.zone.now
else
published_at
end
true
end
def slug
title.to_s.downcase.strip.gsub(" ", "-").gsub(/[^\w-]/, '')
end
def to_param
"#{id}-#{slug}"
end
end
最佳答案
// query =搜索查询字词
// @ rsvps,@ favorites,@ unshows => post_ids数组
Post.search query,
where: {
published_at: {lte: Time.now.in_time_zone("Eastern Time (US & Canada)")},
id: {not: @favorites},
id: {not: @rsvps},
id: {not: @unshows}
}
关于ruby-on-rails - 如何在 Controller 中的searchkick中使用作用域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797144/
首先,我将清理IRB的输出(这是来自生产的)。 我在Rails应用程序中使用了一些搜索支持聚合。在生产中进行了良好的开发测试,但结果却不一致。我的字段中的数据应该清楚地显示在汇总中。 这是我普通的旧A
我正在尝试使用searchkick作为包装器在rails应用程序上实现elasticsearch。 我有一个名为文章的模型,该模型将uuid和title散列为列 我已经在Article模型中添加了se
从这个page,我正在尝试: [15] pry(main)> puts Post.search("price:>600").size Post Search (18.7ms) curl http
我开始使用 searchkick 连接 elasticsearch,但在后台作业中遇到索引更新问题。我在文档中都是通过示例完成的,但是sidekiq中的searchkick任务仅在重新启动sideki
我正在使用 SearchKick 来搜索数据。我正在执行分页。在这种情况下,我如何获得总结果数: 我的搜索结果: #true, :payload=> {:query
我想根据不区分大小写的标题搜索文章。 我试图这样编码 Article.search(where: {title: /some_titles_here/i}, load: false) 但这是行不通的。
我已经通过elasticsearch.rb初始化器使用以下aws_credentials进行设置。 Searchkick.aws_credentials = { credentials:
Searchkick-具有多个模型和字段的自动完成 我正在努力为与Post模型关联的多个模型实现autocomplete functionality。搜索功能可以正常工作,并返回预期的数据。如果我以d
使用aggs时如何使用某些过滤器,而不使用其他过滤器?如果我使用smart_aggs: false,则aggs计数中不使用任何过滤器。 默认情况下,条件适用于聚合。 Product.search "w
我正在尝试使用2列创建多个范围。我知道可以实现多个范围,但是我需要2列才能使用。 在我的模型中,我有一个min_age和max_age。用户可以选择多个年龄来进行过滤,例如:选项为10、11、12、1
我有一个带有searchkick gem的Rails应用程序。我的模型有一个嵌套的JSON字段。我尝试通过word_start匹配使其可搜索。当我明确设置为: class Post "mapper_p
当我有多个匹配的字段时,分数不会增加。 我的搜索方法: search_results = Subscription.search( params[:query], # The sear
在Rails应用程序中,我有post模型和相对的post_locations模型。我们使用searchkick查找数据。当用户在location表中输入位置searchkick搜索以找到该位置时,这就
使用给定的单个属性进行搜索时自动完成功能正常 here . 可以通过->(根据this)自动完成多个属性,例如(名称,城市,国家/地区) def autocomplete Doctor.se
我正在通过searchkick使用Elasticsearch 我有一个名为“详细信息”的字段,使用“标准”分析器进行了分析……现在就像在Google中一样,我们进行"several words"搜索以
我最近更新到High Seirra,不确定这是否意味着什么,现在当我尝试为Active Record模型重新编制索引时遇到此错误。 我尝试过的东西。我已经重新安装了RVM,Ruby,Elasticse
举另一个问题的例子 product = Product.find(10) `raise_record_not_found_exception!' product.nil? => true pro
我正在尝试构建一个高级搜索选项(类似于 Twitter's )。用户可以在搜索查询中输入包含的词、排除的词、包含的词、确切的短语等。 我正在使用 Searchkick 来执行此操作。特殊性Searck
我希望了解多用途搜索的工作原理, 例如,我要搜索手机,左侧菜单返回电子产品相关类别 如果我决定在同一搜索中找到鞋子,左侧菜单返回与衣服相关的类别 像亚马逊或沃尔玛一样,有人知道在同一个搜索中这种部分的
我已经设置了索引的并行重新索引。 Product.reindex(async: {wait: true}) 我在 DelayedJob 中运行该代码并等待,因为替代方案似乎是定期检查完成状态,然后提升
我是一名优秀的程序员,十分优秀!