0

I am trying to create a search form where a user will be able to enter text and the backend will run PgSearch.multisearch in order to retrieve values from a Customer and Account model.

I have set up my two models according to the pg_search gem's page. When I run a PgSearch.multisearch("foo") in my rails console it returns results as expected.

My question is how do I display all relevant results in my view? (Can't seem to figure this one out)

Currently my controller looks like this (params[:search][:token] == "foo"):

def create
   if @search.validate(params[:search])
      @pg_search_documents = PgSearch.multisearch(params[:search][:token])
      respond_with(@pg_search_documents, :location => search_index_path)
    else
      respond_with(@search, :location => search_index_path)
    end
end

In my view I tried:

- @pg_search_documents.each do |pg_search_document|
  = pg_search_document.searchable.id

I keep on getting an error stating "undefined method `each' for nil:NilClass"

What I'd like to achieve is to display all results in my view.

UPDATE 2

Running PgSearch.multisearch("foo") in console returns:

#<ActiveRecord::Relation [#<PgSearch::Document id: 6, content: "foo", searchable_id: 10, searchable_type: "Customer", created_at: "2015-01-20 22:29:31", updated_at: "2015-01-20 22:29:31">]>

When I render PgSearch.multisearch(params[:search][:token]) as text in my browser it only shows a "#".

I figured that this is normal behaviour for a ActiveRecord::Relation, as the same output structures occur when I try to output the results of a AR scope in console vs browser.

UPDATE 3

enter image description here

Can somebody please provide some help?

HermannHH
  • 1,732
  • 1
  • 27
  • 57
  • Have you tried using binding.pry before the @pg_search_documents assignment to see if the params are actually being sent to your create action?. If you can retrieve the data from the console then maybe your problem lies on the view where you have your search form. – La Murga Jan 21 '15 at 05:12
  • I have rendered params[:search] [:token] as text in my controller and the value "foo" was displayed in my browser. @Lamurga – HermannHH Jan 21 '15 at 05:26
  • Hi @Herm, are you querying the same database in the above context as you were within the console? – Drew Jan 21 '15 at 06:38
  • @Drew , I am on my Localhost. With rails s running. – HermannHH Jan 21 '15 at 06:41
  • @Herm, gotcha. Mind having a look within the above context and seeing if the same data is present within whatever tables are relevant within the above context as is present within the console context? – Drew Jan 21 '15 at 06:49
  • @Drew , It does seem to be. What I have noticed is that this GEM returns an activerecord association (similar to Model Scopes). Not sure how to transform this query result into the results that I'm looking for. – HermannHH Jan 21 '15 at 07:31
  • @Herm, gotcha. And gotcha. Also, mind maybe posting the output of `PgSearch.multisearch(params[:search][:token])` within the `create` context as well as the output of `PgSearch.multisearch("foo")` via `rails console`? – Drew Jan 21 '15 at 14:37
  • @Drew , I have added another update to my question. This should answer your question. – HermannHH Jan 21 '15 at 15:40
  • @Herm, thank you! Also, I wonder what output (within `stdout`) would be if you added `puts PgSearch.multisearch(params[:search][:token]).inspect` after the setting of `@pg_search_documents` within `create`? – Drew Jan 21 '15 at 15:50
  • @Drew , I have added a screenshot of my development log. Not sure if this helps you at all. I still classify myself as a rails beginner. Thanks for taking the time to help! I appreciate it – HermannHH Jan 21 '15 at 16:09

0 Answers0