简体   繁体   中英

Thinking Sphinx Rails Multiple Association

I have the following models

class Product < ActiveRecord::Base
  belongs_to :sub_category
end

class SubCategory < ActiveRecord::Base
  belongs_to :category
  has_many :products
end

class Category < ActiveRecord::Base
  has_many :sub_categories , -> { where("activate = 1") }
end

I need to index my products table.I need to search using category name(which is in category table) and subcategory name(in subcategories table)

ThinkingSphinx::Index.define :product , :with => :active_record do
  indexes description
  indexes name
  indexes merchant_name
  indexes sub_category(:sub_category) , :as => :sub_category_name
  indexes category(:name) , :as => :cat_name
  has sub_category_id
end

The category(:name) is failing.The subcategory is working fine. Could somebody please help.I tried sub_category.category(:name) but thats also failing

Error Message

ERROR: index 'link_core': sql_range_query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS cat_name, products.id AS sphinx_internal_id, 'Product' AS `sphinx_internal_' at line 1 (DSN=mysql://root:***@localhost:3306/xxxx_dev_phase4)

name should be passed as a chained method, not as an argument

indexes sub_category.category.name , :as => "category_name"

Thanks to the owner Pat for helping me out

concerned github thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM