Rails Search with query

I want to filter jobs on the parameter passed onto the model, currently search works flawlessly without query passed into the model, but when I type query it doesn't return anything. How can I perform this query with query and criteria. results << model.with_query(query).where(criteria). any idea would be really appreciated.

module Refinery
  class SearchEngine
    # How many results should we show per page
    RESULTS_LIMIT = 100

    # Perform search over the specified models

    def self.search(query, job_region, job_division, country, job_type, page = 1)
      results = []

      Refinery.searchable_models.each do |model|

      criteria = {:job_region => job_region,
                    :job_division => job_division,
                    :country => country,
                    :job_type => job_type

        }.select { |key, value| value.present? }


        if query.present?
          results << model.with_query(query).where(criteria)
        else
          results << model.limit(RESULTS_LIMIT).where(criteria)
        end

      end

      results.flatten[0..(RESULTS_LIMIT - 1)]
    end

  end
end

Answers


The problem here is that the method .with_query(qry) returns an Array. You want to do chain-scoping, so you must use scopes that returns ActiveRecord::Relation objects.

model.with_query(query) 
# returns an Array

model.with_query(query).where(criteria) 
# calling .where on an Array object => NoMethodError

model.where(criteria) 
# returns an ActiveRecord::Relation

model.where(criteria).with_query(query) 
# calls the query on an AR::Relation object, which is doable

Short version: Change this:

results << model.with_query(query).where(criteria)

To this:

results << model.where(criteria).with_query(query)

Need Your Help

Mockery Doctrine / Repository with more than one method

doctrine-orm phpunit mockery

I am using the mockery to test a method that make a lot of doctrine repository invocations with different repositories.

On button click INSERT multiple (textbox and label) texts from many controls inside Table

c# mysql sql .net sql-server-ce

I have created table "TenOperations" which has 4 columns "EUR" type real, "Rate" type real ,"BGN" type real , "Date" type nvarchar