【问题标题】:How to I join 3 tables using Ruby Sequel?如何使用 Ruby Sequel 加入 3 个表?
【发布时间】:2014-03-22 21:26:43
【问题描述】:

我有一个数据库,其中 Notebooks 和 Notes 具有 many_to_many 关系,而 Notes 和 Facts 具有 many_to_many 关系。如何使用 Ruby Sequel 查询与给定 Notebook 关联的 Facts(通过 Note 间接关联)。我应该在 Notebooks 和 Facts 之间创建另一个 many_to_many 关系吗?那会更快吗?

【问题讨论】:

标签: sql ruby join sequel


【解决方案1】:

Sequel 附带一个 many_through_many 插件,允许通过多个连接表在模型之间进行查询:

Notebook.plugin :many_through_many
Notebook.many_through_many :facts,
  :through=>[
      [:notebooks_notes, :notebook_id, :note_id],
      [:notes, :id, :id],
      [:facts_notes, :note_id, :fact_id]
    ]

n = Notebook.first
n.facts

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 2019-10-03
    相关资源
    最近更新 更多