Question

I have a database where Notebooks and Notes have a many_to_many relationship and Notes and Facts have a many_to_many relationship. How do I query the Facts associated with a given Notebook (indirectly associated via a Note) using Ruby Sequel. Should I create another many_to_many relationship between Notebooks and Facts instead? Would that be faster?

Was it helpful?

Solution

Sequel ships with a many_through_many plugin that allows for querying between models through multiple join tables:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top