문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top