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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:01:31+00:00 2026-05-26T06:01:31+00:00

I have a Deal model and one Category model having many to many mapping

  • 0
  • I have a Deal model and one Category model having many to many mapping (has_many:through, as i m saving some related data too.)

  • Also I have a city model which is also having many to many mapping with deal model (Again has_many through)

  • Category model has no relationship with City model.

Now to search deals based upon keywords (Keywords means, i have indexed some columns like title of the deal, description etc)

 @deals = Deal.search(params[:keywords],:per_page => 20, :page => params[:page])

Question is:

Now I need to group these deals based upon the categories, basically first category has 4 deals, second category has 7 deals and so on provided city_id is given.

 @deals = @city.deals.search(params[:keywords],:per_page => 20, :page => params[:page])

PS:
You can think of something like this:
End user will enter Select London as a location and then enter “fun” as a keyword and then i need to search the database to check the london deals having having “fun” keyword and group them based upon the various categories.
I hope i am able to explain the question.

EDIT

Some issues with indexing:
rake thinking_sphinx:rebuild –trace
** Invoke thinking_sphinx:rebuild (first_time)
** Invoke thinking_sphinx:app_env (first_time)
** Execute thinking_sphinx:app_env
** Execute thinking_sphinx:rebuild
** Invoke thinking_sphinx:index (first_time)
** Invoke thinking_sphinx:app_env
** Execute thinking_sphinx:index
Generating Configuration to /Users/me/projects/deals/config/development.sphinx.conf
rake aborted!

Cannot automatically map attribute category_ids in Deal to an
equivalent Sphinx type (integer, float, boolean, datetime, string as ordinal).
You could try to explicitly convert the column's value in your define_index
block:
  has "CAST(column AS INT)", :type => :integer, :as => :column
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:334:in `translated_type_from_database'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:170:in `type'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:139:in `include_as_association?'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/attribute.rb:107:in `to_select_sql'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:69:in `block in sql_select_clause'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:69:in `collect'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:69:in `sql_select_clause'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source/sql.rb:19:in `to_sql'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source.rb:117:in `set_source_sql'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/source.rb:51:in `to_riddle_for_core'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:114:in `block in to_riddle_for_core'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:113:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:113:in `each_with_index'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:113:in `to_riddle_for_core'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/index.rb:83:in `to_riddle'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/active_record.rb:245:in `block in to_riddle'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/active_record.rb:244:in `collect'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/active_record.rb:244:in `to_riddle'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:164:in `block in generate'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:161:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:161:in `generate'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/configuration.rb:177:in `build'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/tasks.rb:78:in `block (2 levels) in <top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.5/lib/thinking_sphinx/tasks.rb:95:in `block (2 levels) in <top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:90:in `block (2 levels) in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:90:in `block in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:62:in `block in run'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/Users/me/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/bin/rake:32:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
Tasks: TOP => thinking_sphinx:index
  • 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-26T06:01:31+00:00Added an answer on May 26, 2026 at 6:01 am

    I supposed that you have a Deal model like

    class Deal < ActiveRecord::Base
      has_many :category_deals
      has_many :categories, :through => :category_deals
      has_many :city_deals
      has_many :cities, :through => :city_deals
    
      define_index do
        indexes :name
        has category_deals.category_id, :as => :category_ids
        has city_deals.city_id, :as => :city_ids
      end
    end
    

    So you want to search by city and given category or categories (1,3,2) for the given city with categories ASC order:

    Deal.search(params[:keywords], :with => {:category_ids => [1,3,2], :city_ids => params[:city_id]}, :order => "category_ids ASC", :per_page => 20, :page => params[:page])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a deal model that contains two date field. First one is start_date
Here is the deal I have a one to many relationship between User and
Like many of you, I have to deal with a large amount of files:
I want to have one model & view that is served by multiple controllers
One universal data model author I am reading contends that in his experience, 50%
I am relatively new to Rails. I have a User model through Devise. I
I wonder what the best practice is to deal with a data model like
I want to model XBRL (consisting of one XSD and many included XSD files
I have a simple data model and I am using EF4.1 code first (but
I'm new to Django and I have some problems to gasp how to deal

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.