B
B
Bogdan2017-09-22 13:40:46
Ruby on Rails
Bogdan, 2017-09-22 13:40:46

ActiveRecord relationship across multiple fields?

How to properly configure ActiveRecord to write the query correctly, there are 2 tables Table1 ( id1, id2) and table Table2 ( id1, id2, enabled ).
You want the output to be like this:

SELECT table1.id1, table1.id2 FROM table2 LEFT JOIN table1 on table1.id1 = table2.id1 and table1.id2 = table2.id2
WHERE table2.enabled = true

Of course, you can register in the rails manually, but how can you do it through activerecord? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2017-09-22
@2ord

More or less like this:

Table2Model.
  joins("LEFT JOIN table1 on table1.id1 = table2.id1 and table1.id2 = table2.id2").
  # и т.д.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question