ruby on rails - Searching multiple models with pg_search -


i trying implement full text search in rails app. using postgresql database, therefore, using postgresql's full text search capabilities. have 2 models users , scoreboards , want search through both models against specific columns pull out records.

i using pg_search gem full text search, read multisearch documentation , implemented successfully. have created pg_search_documents table. code given below.

scoreboard model

include pgsearch    multisearchable :against => [:name_of_organization, :name_of_activity, :name_of_scoreboard] 

user model

include pgsearch   multisearchable :against => :name 

controller code

 def index    @pg_search_documents = pgsearch.multisearch(params[:search])  end 

the search works when run code <%= pg_search.searchable %> in view. returns both user , scoreboard objects when each 1 of them searched.

view code

<% @pg_search_documents.each |pg_search| %>         <%= pg_search.searchable.name %>         <%= pg_search.searchable.name_of_organization %> <% end %> 

however, when try run view code given above , search name, following error,

undefined method `name_of_scoreboard' #<user:0x007fa44ad18cd8>.  

i similar error when try search name_of_scoreboard,

`undefined method `name' #<scoreboard:0x00000006fa3560>.`  

if use following code in view, <%= @pg_search_documents.searchable.id %>, id's both user , scoreboard show up. means can search against columns common in 2 models.

if try search 2 different columns such name , name_of_scoreboard, 2 errors mentioned above. able search between 2 different models not able display results view. not sure if doing wrong or maybe missing important. not sure. have tried keep brief , tried include relevant information. if specific code required, please let me know. always, appreciated!!

i think you're mixing "searching for" , displaying column results of - looks code searching across different fields in multiple models. search returns array of "documents" point matching objects. issue you're having you're trying display name of object doesn't have name. think following you're going for.

<% @pg_search_documents.each |pg_search| %>   <% if pg_search.searchable.respond_to?(:name) %>     <%= pg_search.searchable.name %>   <% else %>     <%= pg_search.searchable.name_of_organization %>   <% end %> <% end %> 

that display name if result object has name (ie - if user) , otherwise organization name (if scoreboard). note, isn't great solution since it's introducing logic view explains issue.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -