Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using associated_against and multisearchable together #34

Closed
cbillen opened this issue May 2, 2012 · 3 comments
Closed

Using associated_against and multisearchable together #34

cbillen opened this issue May 2, 2012 · 3 comments

Comments

@cbillen
Copy link

cbillen commented May 2, 2012

I want to use Multisearch along side associated against so for instance in my imaginary example:

class Order < ActiveRecord::Base
     multisearchable :against => [:reference_number, { :shipping_address => [:person_name_override, :company_name_override] }]
end

Then be able to run

PgSearch.multisearch('search_term') 

and have it go against the associated against model, is that possible to achieve?

@nertzy
Copy link
Collaborator

nertzy commented May 5, 2012

No, because the multisearch feature uses a polymorphic association, which cannot be traversed in SQL.

The best way to achieve what you want is to make a method on the Order class that returns the text that you want to be searchable.

For your example, it would be something like:

class Order < ActiveRecord::Base
  has_one :shipping_address

  multisearchable :against => [:reference_number, :overridden_names]

  def overridden_names
    "#{shipping_address.person_name_override} #{shipping_address.company_name_override}"
  end
end

To make it stay consistent, you'll need to add :touch => true to the ShippingAddress class's association so that when it is updated, the pg_search Active Record callbacks get fired on its Order as well. This will also change the updated_at on the Order, in case that's a concern for you.

class ShippingAddress < ActiveRecord::Base
  belongs_to :order, :touch => true
end

@nertzy nertzy closed this as completed May 5, 2012
@bcackerman
Copy link

In this case I'm getting a

PG::UndefinedColumn: ERROR: column orders.overridden_names does not exist

@bcackerman
Copy link

A fix #70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants