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 540145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:12:35+00:00 2026-05-13T10:12:35+00:00

Model: Product has-many product-categories, :through => … Question 1) How do I index a

  • 0

Model: Product

has-many product-categories, :through => ...

Question 1) How do I index a many to many association with thinking sphinx

Must I use has?

Questions 2) How is this searched in the controller

ex. Product.search params[:search-params], :conditions => {some_conditions}

  • 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-05-13T10:12:35+00:00Added an answer on May 13, 2026 at 10:12 am

    I’ve not tried this on a has_many :through so shoot me down in flames if you have, but I don’t see why this wouldn’t work for you too, (I’m using it on a has_many association) you basically use your association in the index definition. Then searches against that model will also search the child records.

    class Product < ActiveRecord::Base
      has_many :product_categories
    
      define_index do
        indexes a_product_field_to_index
        indexes product_categories.name, :as => :categories
      end
    end
    

    In the controller:

    @products = Product.search(params[:query] || '')
    #params[:query] is simply the search string, I can't remember if you need to sanitize this, I would always assume you do unless you find out otherwise
    

    In the view:

    @products.each do |p| 
      p.categories.each do |cat|
      end
    end
    

    If you don’t already have it I would highly recommend the thinking-sphinx book available on peepcode: https://peepcode.com/products/thinking-sphinx-pdf

    Hope that helps.

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

Sidebar

Related Questions

I have Product model and it has many categories with a has_many :through association
I'm building a Django site. I need to model many different product categories such
Let's assume I have a model called product. Let's assume that product has three
Let's say I have this model named Product with a field named brand .
My Invoice model has an address_id attribute, and I don't want this address_id to
I am having many products with many categories which are associated with has_many using
I am trying to set up a Many-to-Many association between 2 objects. I have
I am using Rails 3. The main model is Product :product has_many :images :product
I have the following models: class Product < ActiveRecord::Base belongs_to :brand belongs_to :model accepts_nested_attributes_for
My model layer is being used by a handful of different projects and I'd

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.