Consider that we have two models;
Say, we’d like to implement a filtration mechanism that allows us to query the wells – and for those set of wells we’d like to conditionally present the set of survey_points for those set of filtered wells.
A method ‘
Using the power of AREL chaining, we could then conditionally extract the SurveyPoints using the following:
This roughly translates into the query:
The challenge here is that if both results is large, depending on the cardinality – the database engine might not be able to make use of defined indexes and the outer query might not be optimizable.
Instead – if we use pluck instead of select:
Rails would fire two queries instead of an inner one. Pluck would first extract the list of ids (val_1, val_2, …) in an array before passing it to the main query. This roughly translates into:
No inner query is used in that case and the main one is easy to optimize.