Cannot search related records using multisearch due to the way polymorphic associations work in Rails and SQL.
I will add an error explaining the situation, so that in the future it will not be so confusing.
Sorry for the confusion.
Instead, you can define a method in Car that returns the text you want to execute.
class Car < ActiveRecord::Base include PgSearch multisearchable :against => [:name, manufacturer_name] belongs_to :manufacturer def manufacturer_name manufacturer.name end end
Or, to be even more concise, you can delegate:
class Car < ActiveRecord::Base include PgSearch multisearchable :against => [:name, manufacturer_name] belongs_to :manufacturer delegate :name, :to => :manufacturer, :prefix => true end
But you have to make sure that the pg_search_documents table is updated if you ever made a name change with a manufacturer instance, so you should add :touch => true to your association:
class Manufacturer < ActiveRecord::Base has_many :cars, :touch => true end
Thus, it will call Active Record callbacks in all car records when the manufacturer updates, which will call pg_search callback to update the searchable text stored in the corresponding pg_search_documents record.
nertzy
source share