Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何过滤大量记录并从postgres ltree结构中仅获取最外层的记录?_Ruby On Rails_Postgresql_Ltree - Fatal编程技术网

Ruby on rails 如何过滤大量记录并从postgres ltree结构中仅获取最外层的记录?

Ruby on rails 如何过滤大量记录并从postgres ltree结构中仅获取最外层的记录?,ruby-on-rails,postgresql,ltree,Ruby On Rails,Postgresql,Ltree,我将数据库记录安排在ltree结构中(Postgres-ltree扩展) 我想将这些项目筛选到当前选择的最外层祖先 测试用例: [11, 111, 1111, 2, 22, 222, 2221, 2222] => [11, 2]; [1, 11, 111, 1111, 1112, 2, 22, 222, 2221, 2222, 3, 4, 5] => [1, 2, 3, 4, 5]; [1111, 1112, 2221, 2222]

我将数据库记录安排在
ltree
结构中(Postgres-ltree扩展)

我想将这些项目筛选到当前选择的最外层祖先

测试用例:

 [11, 111, 1111, 2, 22, 222, 2221, 2222]                   => [11, 2];
 [1, 11, 111, 1111, 1112, 2, 22, 222, 2221, 2222, 3, 4, 5] => [1, 2, 3, 4, 5];
 [1111, 1112, 2221, 2222]                                  => [1111, 1112, 2221, 2222];
我已经在Ruby中实现了这一点

  def fetch_outer_most_items(identifiers)
    ordered_items = Item.where(id: identifiers).order("path DESC")
    items_array = ordered_items.to_a
    outer_most_item_ids = []

    while(items_array.size > 0) do
      item = items_array.pop
      outer_most_item_ids.push(item.id)
      duplicate_ids = ordered_items.where("items.path <@ '#{item.path}'").pluck(:id)

      if duplicate_ids.any?
        items_array = items_array.select { |i| !duplicate_ids.include?(i.id) }
      end
    end

    return ordered_items.where(id: outer_most_item_ids)
  end
def fetch_outer_most_项目(标识符)
ordered_items=项目。其中(id:标识符)。顺序(“路径描述”)
items\u array=有序的\u items.to\u a
外部\u most\u项目\u ID=[]
而(items_array.size>0)不
item=items\u array.pop
外部\u most \u项目\u id.push(项目id)
重复的\u id=有序的\u项目。其中(“items.path
  def fetch_outer_most_items(identifiers)
    ordered_items = Item.where(id: identifiers).order("path DESC")
    items_array = ordered_items.to_a
    outer_most_item_ids = []

    while(items_array.size > 0) do
      item = items_array.pop
      outer_most_item_ids.push(item.id)
      duplicate_ids = ordered_items.where("items.path <@ '#{item.path}'").pluck(:id)

      if duplicate_ids.any?
        items_array = items_array.select { |i| !duplicate_ids.include?(i.id) }
      end
    end

    return ordered_items.where(id: outer_most_item_ids)
  end