问题
In my app, users can collect favorite Documents. I want to return a list of documents most relevant to those they have favorited, using a related_documents join table that has a relevance_score:integer for the relationship between a document and a related document.
TLDR; see the update at the bottom for an almost-working query.
Without success, I have tried various queries like:
some_user.favorited_documents.joins(:related_to_documents)
.select("docid_id FROM documents.related_documents")
.order("related_documents.relevance_score DESC")
.limit(5 * some_user.favorited_documentss.count)
Ideally I'd like to return the top 5 most relevant related documents for each favorited document, but I'd compromise with just the top (5*favorited_documents) overall, as the relevance scores are relatively comparable.
My models look like:
class User
has_many :favorited_events
has_many :favorited_documents, through: :favorited_events, source: :document
class Document
# id
# docid ← this is a unique organisational doc identifier
has_many :related_documents, order: "relevance_score DESC"
has_many :related_to_documents, through: :related_documents, source: :docid,
order: "related_documents.relevance_score DESC"
has_many :related_by_documents, class_name: "RelatedDocument", foreign_key: :docid_id,
primary_key: :docid
class RelatedDocument
# id : integer
# document_id : integer
# docid_id : integer
# relevance_score : integer
belongs_to :document
belongs_to :docid, class_name: "Document", primary_key: :docid
Notes:
The Document model has both an id and a docid, the later is a unique organisational identifier for our documents. dicid is used by RelatedDocument to track related_documents as these are not always already in the documents table in this database / instead they're retrieved as needed from another system.
For complex business reasons, related_documents is a one-way relationship / hence use of different primary keys (document.id vs document.docid).
Thanks for any help!
UPDATE:
Okay, I have a mostly-working query now: (uniq is ignored??)
Document.joins("INNER JOIN related_documents ON
documents.docid = related_documents.docid_id")
.select("documents.*, related_documents.relevance_score")
.where("related_documents.document_id IN (?)",
some_user.favorited_documents)
.order("related_documents.relevance_score DESC")
.uniq
.limit(10)
Which returns the top 10 most 'relevant' documents related to some_user's favorited documents.
The only part that isn't working is the 'uniq'. Although SELECT DISTINCT appears in the SQL the returned documents include duplicates. Any ideas?
来源:https://stackoverflow.com/questions/34014637/activerecord-query-to-return-related-documents-using-relateddocuments-join-table