I'm using MySQL and I'm trying to create a couple of scopes to find a User based on a set of conversations, users and conversations have a polymorphic association that allows the user to author and receives conversations.
The "with_conversations" scope is doing what I want, however, it's not very efficient, I'm having trouble making it execute a single query instead of the addition 2 arrays, which triggers 2 separate queries.
class User
scope :with_received_conversations, -> (conversations) { joins(:received_conversations).where(conversations: { id: conversations, receiver_type: "User" }) }
scope :with_authored_conversations, -> (conversations) { joins(:authored_conversations).where(conversations: { id: conversations, author_type: "User" }) }
scope :with_conversations, -> (conversations) { with_authored_conversations(conversations) | with_received_conversations(conversations) }
end
I attempted using the "or()" active record function but it returns the following error:
*** ArgumentError Exception: Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
nil