简体   繁体   English

思维狮身人面像Rails多重关联

[英]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. 类别(:name)失败。子类别运行正常。 Could somebody please help.I tried sub_category.category(:name) but thats also failing 有人可以帮忙吗。我尝试了sub_category.category(:name)但那也失败了

Error Message 错误信息

ERROR: index 'link_core': sql_range_query: You have an error in your SQL syntax; 错误:索引'link_core':sql_range_query:您的SQL语法有错误; 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) 检查与您的MySQL服务器版本相对应的手册,以在'AS cat_name,products.id AS sphinx_internal_id,'Product'AS'sphinx_internal_'第1行附近使用正确的语法(DSN = mysql:// root:*** @本地主机:3306 / xxxx_dev_phase4)

name should be passed as a chained method, not as an argument name应作为链接方法而不是参数传递

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

Thanks to the owner Pat for helping me out 感谢店主Pat的帮助

concerned github thread 有关github线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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