S
S
Semyon Pyatnitsky2016-07-17 13:19:57
ORM
Semyon Pyatnitsky, 2016-07-17 13:19:57

How to rewrite query in ORM Ruby on Rails?

Hello! Help with links in models
There are 3 tables: Clients, Contacts, Calls
The Calls table has both ClinetId and ContactId
There is a request

SELECT `calls`.* 
FROM `calls` WHERE `calls`.`CallContragent` = '130350' 
OR `calls`.`CallContact` 
IN ('74031', '74032', '74214', '74221', '74222', '74243', '74256', '74273', '74293', '74294', '74296', '74323', '74327', '74535', '74536')

The query displays correctly how to make a connection in the model
1 If I do has_many :calls, class_name: 'Call', primary_key: 'ClientId',foreign_key: 'CallContragent' - Then it only displays calls on CallContragent
2 If I do like this has_many :calls, class_name: 'Call', primary_key: 'ClientId',foreign_key: 'CallContragent', through: :contacts - it displays calls via CallContact and does not display communication
with
bab2da039404416dabc17ff9cda2084f.jpgee4812fb22964029b193e46beec27522.jpg
CallConragent

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2016-07-17
@snowsem

your request does not need links, like this

Call.where('CallContragent' => 123).or.where('CallContact' => [123, 123, 123]) # rails 5
Call.where('CallContragent = ? OR CallContact IN ?', 123, [123, 123, 123]) # rails 4

In general, in the Call model, apparently, there should be
belongs_to :client
belongs_to :contact

And in Client
has_many :calls
has_many :contacts, through: :calls

In Contact - something similar to that with Client, only for calls and clients

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question