I'm 看一看铁路舱单碎块中的这一代码,定在第12行“项目”中是个范围物体。 然后,在接下来的几条线路上,似乎分门别类的物体被多次重新分配。
When I run the code it seems only 1 query is being made wwhich is the sum total of all the scopes..(which is great, just not sure why). Does that mean that a scoped object accumulates scopes until the end of the method when it is executed? Or does Active Record just optimize performance by minimizing the number of queries needed by waiting for all the queries to be gathered... help me understand :) :
def get_autocomplete_items(parameters)
model = parameters[:model]
term = parameters[:term]
method = parameters[:method]
options = parameters[:options]
scopes = Array(options[:scopes])
limit = get_autocomplete_limit(options)
order = get_autocomplete_order(method, options, model)
items = model.scoped
scopes.each { |scope| items = items.send(scope) } unless scopes.empty?
items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
items = items.where(get_autocomplete_where_clause(model, term, method, options)).
limit(limit).order(order)
end