【问题标题】:Recursively fetch records in Rails在 Rails 中递归获取记录
【发布时间】:2019-09-21 15:26:39
【问题描述】:

在我的应用程序中,可以通过执行以下操作找到员工的所有团队报告人

User.where(primary_reporter: "some unique code of a user")

其中主要报告者是需要获取其团队报告者的用户。

但是我现在试图递归地找到所述用户的报告者和他的团队成员的报告者形成一个层次结构图。在那里找不到任何东西。

我的用户模型是这样的:

class User
  has_many :reportees, lambda { |user| includes(:actor).where [" . 
  (users.status = 1)"] }, class_name: 'User', foreign_key: 
  'primary_reporter'
end

class Actor
  has_one: user
end

我正在为数据库使用 postgres

【问题讨论】:

标签: sql ruby-on-rails ruby postgresql


【解决方案1】:

如果您想在 postgres 中使用 rails 实现分层实现,那么您可以尝试在 postgres 中使用ltree 扩展,您也可以在其中使用索引(用于加快查询)。我们还有 ruby​​ gem,使用这个 gem 我们可以很容易地进行查询。

参考文献

Postgres:https://www.postgresql.org/docs/9.1/ltree.html

导轨:https://github.com/cfabianski/ltree_hierarchy

【讨论】:

    猜你喜欢
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多