加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ActiveRecord has_and_belongs_to_many:查找

发布时间:2020-12-17 03:17:27 所属栏目:百科 来源:网络整理
导读:我正在实现一个使用名称,标签和位置的搜索系统.服务器和标签之间存在has_and_belongs_to_many关系.这是我的搜索方法目前的样子: def self.search(params) @servers = Server.all if params[:name] @servers = @servers.where "name ILIKE ?","%#{params[:na
我正在实现一个使用名称,标签和位置的搜索系统.服务器和标签之间存在has_and_belongs_to_many关系.这是我的搜索方法目前的样子:

def self.search(params)
  @servers = Server.all

  if params[:name]
    @servers = @servers.where "name ILIKE ?","%#{params[:name]}%"
  end

  if params[:tags]
    @tags = Tag.find params[:tags].split(",")
    # How do I eliminate servers that do not have these tags?
  end

  # TODO: Eliminate those that do not have the location specified in params.
end

tags参数只是以逗号分隔的ID列表.我的问题在if params [:tags]条件块的注释中说明.如何消除没有指定标签的服务器?

奖金问题:有什么方法可以加快速度吗?所有字段都是可选字段,我只使用Postgres.

编辑

我找到了一种方法来做到这一点,但我有理由相信它的运行速度会非常慢.有什么方法比我做的更快?也许是一种让数据库完成工作的方法?

tags = Tag.find tokens
servers = servers.reject do |server|
    missing_a_tag = false

    tags.each do |tag|
        if server.tags.find_by_id(tag.id).nil?
            missing_a_tag = true
        end
    end

    missing_a_tag
end

解决方法

使用所有给定标签检索服务器

if params[:tags]
  tags_ids = params[:tags].split(',')
  @tags = Tag.find(tags_ids) 
  @servers = @servers.joins(:tags).where(tags: {id: tags_ids}).group('servers.id').having("count(*) = #{tags_ids.count}")
end

具有(…)部分的组(…)选择具有所有请求标签的服务器.如果您正在寻找至少包含其中一个标签的服务器,请将其删除.

使用此解决方案,搜索在单个SQL请求中完成,因此它将比您的解决方案更好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读