Attribuite 2 relazioni ActiveRecord che generano seguenti SQL:Come unire (link) 2 Relazioni su diversi tavoli in Rails 4
- relazione
a = SELECT comments.* FROM comments INNER JOIN attachments ON attachments.comment_id = comments.id WHERE attachment.name ILIKE '%foo%
- relazione
b = SELECT attachments.* FROM attachments INNER JOIN users ON attachments.user_id = users.id WHERE users.other_conditions
Questo ha funzionato in Rails/ActiveRecord :
puts a.merge(b).to_sql # Rails 3
> "SELECT comments.* FROM comments INNER JOIN attachments ON attachments.comment_id = comments.id INNER JOIN users ON attachments.user_id = users.id WHERE attachment.name ILIKE '%foo% AND users.other_conditions"
Penso che abbia funzionato perché lo merge
ignorava qualsiasi associazione non esistente nelle query.
Ma Rails è molto più pedante e fallisce con:
puts a.merge(b).to_sql # Rails 4
> ActiveRecord::ConfigurationError: Association named 'user' was not found on Comment; perhaps you misspelled it?
Quindi la domanda è come posso letteralmente unire i 2 rapporti senza rotaie di essere preoccupato per la correttezza (mie specifiche assumersi la responsabilità per quella)?
Controlla la gemma Arel utilizzata internamente da Rails - https://github.com/rails/arel e alcuni esempi/specifiche qui https://github.com/rails/arel/tree/master/test potresti ottenere alcuni più intuizione. –
@OtoBrglez non riesce a trovare alcun puntatore lì. Hai qualche? –