Reputation: 1032
I want to use the SQL clause "Use index('index_name')" to an ActiveRecord query, does anyone knows a good way to do it with Activerecord I wanted to avoid adding string directly to the query.
Upvotes: 5
Views: 1785
Reputation: 505
Just as a follow up, I've just implemented a solution based on James' above but used a scope instead:
scope :force_index, -> (index) {
from("#{table_name} FORCE INDEX(#{index})")
}
Upvotes: 0
Reputation: 6981
I use this in my models:
def self.use_index(index)
from("#{self.table_name} USE INDEX(#{index})")
end
Upvotes: 3