Is is possible in Rails > 3.2 to add conditions to the join statement generated by the includes
method?
Let's say I have two models, Person and Note. Each person has many notes and each note belong to one person. Each note has an attribute important
.
I want to find all the people preloading only the notes that are important. In SQL that will be:
SELECT *
FROM people
LEFT JOIN notes ON notes.person_id = people.id AND notes.important = 't'
In Rails, the only similar way to do that is using includes
(note: joins
won't preload notes) like this:
Person.includes(:notes).where(:important, true)
However, that will generate the following SQL query which returns a different result set:
SELECT *
FROM people
LEFT JOIN notes ON notes.person_id = people.id
WHERE notes.important = 't'
Please, notice that the first resultset includes all the people and the second one only the people associated to important notes.
Also notice that :conditions are deprecated since 3.1.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…