Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:03:44+00:00 2026-06-03T23:03:44+00:00

I’m adding pg_search into a Rails app. I’m not completely understanding the configuration, and

  • 0

I’m adding pg_search into a Rails app. I’m not completely understanding the configuration, and would appreciate a gentle nudge in the right direction.

First, I already have a multi model site more or less set up and running on my app. But I want to extend it to also search on associated models.

For example, I have Manufacturer, Car, Model classes. Currently if I search for “Ford”, only the manufacturer is returned. I’d also like to return all the associated Cars (which belong to Manufacturer) and Models (which belong to Car).

I can see how to do this as a scoped search

class Car
  pg_search_scope :manufactured_by, :associated_against => {
    :manufacturer => [:name]
  }
end

But if I try to do this on a multisearch it doesn’t work

class Car
  include PgSearch
  multisearchable :against => [:name], 
    :associated_against => {
        :manufacturer => [:name]
      }
end

It doesn’t generate an error, it simply doesn’t pick up the associated records.

I have a feeling I’m missing something fundamental in my understanding of how this all fits together. I’d really appreciate if someone could help me understand this, or point me towards a good source of info. I’ve been through the info on github and the related Railscast, but I’m still missing something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:03:45+00:00Added an answer on June 3, 2026 at 11:03 pm

    It is impossible to search associated records with multisearch, due to how polymorphic associations work in Rails and SQL.

    I will add an error that explains the situation so that in the future it won’t be as confusing.

    Sorry for the confusion.

    What you could do instead is define a method on Car that returns the text you wish to search against.

    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 succinct, you could 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 the pg_search_documents table gets updated if you ever make a name change to a Manufacturer instance, so you should add :touch => true to its association:

    class Manufacturer < ActiveRecord::Base
      has_many :cars, :touch => true
    end
    

    This way it will call the Active Record callbacks on all the Car records when the Manufacturer is updated, which will trigger the pg_search callback to update the searchable text stored in the corresponding pg_search_documents entry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.