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

  • Home
  • SEARCH
  • 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 378839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:50:00+00:00 2026-05-12T14:50:00+00:00

I am having many products with many categories which are associated with has_many using

  • 0

I am having many products with many categories which are associated with has_many using memberships.

I am trying to create a search box where any one can search products while also filter their search with a category dropdown (so only products with relevant categories can be retrieved).

The thinking_sphinx index is in Product model
I don’t get any errors but dropdown does not effect the search.



MODEL: 
    has_many :memberships,:dependent=> :destroy
    has_many :categories, :through => :memberships
    named_scope :published, :conditions => {:publish => 1}
    define_index do
        indexes product_name
        indexes product_description
        indexes publish
        indexes memberships.product_id
        indexes memberships.category_id
        indexes categories.category_name
      end
    end

CONTROLLER: 
     @products = Product.search params[:search],:conditions=>{@product.memberships.category_id =>params[:category_product] },:page=> params[:page] || 1,:per_page =>4


VIEW:
           form_tag search_path, :method =>:get do 
           text_field_tag :search, params[:search]
           form_tag categories_path, :method => :get do 
           select_tag"category", options_from_collection_for_select (Category.find (:all,   :group=>:id), :id, :category_name,params[:category_product]) 
           end 
           submit_tag "search", :name => nil 
           end 
  • 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-12T14:50:01+00:00Added an answer on May 12, 2026 at 2:50 pm

    You need to use an attribute for filtering. In your define_index use a ‘has’ method, and in the search use a :with params. Something like :

    define_index do
      …
      has categories(:id), :as => categories_id
      …
    end
    

    and the search would be :

    Product.search params[:search], :with => { :categories_id => params[:category] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a web application using php, I had many users having
I'm having some issues with a many-to-many relationship I am trying to create. The
I'm having (many, but for now,) one problem with a project I'm working on.
I'm just wondering if anyone has had any problems having too many subscribers for
I'm trying to create an inventory form that sorts the products based on their
I have a Database storing details of products which are taken from many sites,
I'm having a problem with an :has_many => :through association which references another :has_many
I am having problem with updating data to database. I have a simple many-to-one
I have 2 tables having many to many relation and hence there is junction
I am creating a simple Android widget with a typical usecase of having many

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.