Activerecord Force Index includes:

I am trying to create an ActiveRecord statement that forces an index, and also includes.

The operation looks something like this:

Job.from("jobs FORCE INDEX(index1,index2)").includes(:workflow)

however, it seems AR doesn't like it and throws:

NoMethodError (undefined method `map' for "jobs FORCE INDEX(index1,index2)":Arel::Nodes::SqlLiteral):

Does anyone have any ideas? Everything works fine if the include statement is cleared, so I assume it is trying to create a query LEFT OUTER JOINand aliasing tables ...

Thank you, m

+4
source share
1 answer

In accordance with the comments on the question, it turns out that calling a countchain with fromand includes(with the condition for the association included) is a problem.

, from, from("jobs"), , .

includes ( ). , , joins includes ( !). ( , ):

 q = Job.from("jobs FORCE INDEX(index1,index2)")
 q = q.joins(:workflow)
 q = q.where("workflows.name = 'Bob'")
 q.count

, includes, :

 q = Job.from("jobs FORCE INDEX(index1,index2)")
 q = q.joins("left outer join workflows on workflows.job_id = jobs.id")
 q = q.where("workflows.name = 'Bob'")
 q.count
+1

Source: https://habr.com/ru/post/1584269/


All Articles