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

A question about searching associations not a bug #534

Open
scopethis opened this issue May 21, 2024 · 0 comments
Open

A question about searching associations not a bug #534

scopethis opened this issue May 21, 2024 · 0 comments

Comments

@scopethis
Copy link

scopethis commented May 21, 2024

Hi, I'm just getting my head around pg_search and I have a question regarding associations.

Given the following simple setup

class Book < ActiveRecord::Base
  has_many :reviews
end

class Review < ActiveRecord::Base
  belongs_to :book
end

There is no joining table in this scenario. Just two tables: books and reviews.
If I wanted to grab all reviews and their associated book. I could do something like

Review.joins(:book).select('reviews.*', 'books.*')

This should return a list of repeated book names with their review.
I assumed I would be able to do something like:

class Review < ActiveRecord::Base
  belongs_to :book

  pg_search_scope :search, 
    against: {comment: 'A'},
    associated_against: {
       book: {name: 'B'}
    },
    using: { tsearch: { dictionary: 'english' } }
end
Review.joins(:book).select('reviews.*', 'books.*')
  .search('Harry Potter and the Deathly Hallows is a page turner.')

Is that possible? Or have i missed something?

@scopethis scopethis changed the title A question about associations not a bug A question about searching associations not a bug May 21, 2024
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

1 participant