0

For example:

class User < ActiveRecord::Base
  has_many :cars
end

#  name       :string(255)
class Car < ActiveRecord::Base
  belongs_to :user
end

How would you use pg_search to do a full text search of the names of cars that only a single User instance has (versus all cars)?

TenJack
  • 1,594
  • 4
  • 21
  • 35

1 Answers1

1

I'm the author of pg_search. It sounds like what you want to do is chain a pg_search_scope onto an association:

class User < ActiveRecord::Base
  has_many :cars
end

class Car < ActiveRecord::Base
  include PgSearch
  belongs_to :user
  pg_search_scope :search_by_name, against: :name
end

user = User.find(1)
user.cars.search_by_name("Ford")
Grant Hutchins
  • 4,275
  • 1
  • 27
  • 32
  • Can this work also with multisearch? I was looking at `odyssey = EpicPoem.create!(:title => "Odyssey", :author => "Homer") rose = Flower.create!(:color => "Red") PgSearch.multisearch("Homer")` and if `user = User.first` it doesn't appear there's any way to scope `PgSearch` on `user.` – Ryan Buckley Aug 30 '17 at 18:35
  • No, you would need to set up a separate `pg_search_scope` on User to do that. – Grant Hutchins Aug 31 '17 at 20:59