Search code examples
ruby-on-railsmeta-search

stop meta_search doing sql to early


in the docs it's said:

MyObject.search()

returns an instance of MetaSearch::Builder (something like ActiveRecord::Relation). But in my case when I do this I get a collection of objects because the sql-query is send to the database.

I would like to have something like this:

search = MyObject.search() # no sql-query should be done here

count = search.count # count sql done

objects = search.all # select sql done - maybee with pagination

does anyone know how to stop Meta_search from doing the the query to early?

-> ok, something mysterious going on in my shell:

search = MyObject.search() # queries the database
search = MyObject.search(); 0 # stores a MetaSearch-Object in search

the console seems to call an extra method after each comand


Solution

  • If you're testing in irb, be aware that returned objects are inspected. In the case of a MetaSearch builder, this means the relation gets inspected. If you have a look at ActiveRecord's inspect method, in relation.rb, you'll see that it calls to_a, which executes the query and returns the results.