[英]Problems using Thinking Sphinx in Ruby on Rails with multiple models
I'm developing a website on Ruby on Rails with the search engine Sphinx (I'm using Thinking Sphinx). 我正在使用搜索引擎Sphinx在Ruby on Rails上开发一个网站(我正在使用Thinking Sphinx)。 I have a model in which I want to make the searches and I'm using another models (I made the relationships in the models and in the tables) but I want to make additional INNER JOINS, so, I have something like this:
我有一个要在其中进行搜索的模型,并且我正在使用其他模型(我在模型和表中建立了关系),但是我想创建其他INNER JOINS,所以,我有这样的东西:
class Group < ActiveRecord::Base
belongs_to :person
has_many :categories, :dependent => :destroy
define_index do
indexes group_name
indexes person.fullnameindexes categories.category_name
indexes categories.category_name
end
end
It's ok to make something like this? 做这样的事情可以吗?
class Group < ActiveRecord::Base
belongs_to :person
has_many :categories, :dependent => :destroy
define_index do
indexes group_name
indexes person.fullnameindexes categories.category_name
indexes categories.category_name
indexes subcategories.subcategory_name #additional table
end
end
As you can see, I'm adding a new model (Subcategory) that has no relationship with the model Group, but it has a relationship with the model Category, is this ok? 如您所见,我要添加一个新模型(子类别),该模型与模型组没有关系,但是与模型类别有关系,可以吗? or what is the right way to do that?
或正确的方法是什么?
Those are the links I'm following: 这些是我关注的链接:
http://freelancing-god.github.com/ts/en/indexing.html http://freelancing-god.github.com/ts/en/indexing.html
http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx
If subcategory is referenced in the Category model, you can do this: 如果在类别模型中引用了子类别,则可以执行以下操作:
indexes categories.subcategories.subcategory_name, :as => :subcategory_names
Thinking Sphinx will happily go through associations into deeper associations if you want it to. 如果需要的话,认为Sphinx将很高兴通过关联进入更深层次的关联。
I think the short answer to this is "no". 我认为对此的简短答案是“否”。 ThinkingSphinx will try to reference an association on
Group
named subcategories
, which won't exist, and you should get an error when indexing. ThinkingSphinx将尝试引用
Group
名为subcategories
的关联,该关联将不存在,并且在建立索引时会出现错误。
If Category has_many :subcategories
, you can express this in Group
with a :through
option: 如果
Category has_many :subcategories
,则可以使用:through
选项在Group
表示:
class Group < ActiveRecord::Base
belongs_to :person
has_many :categories, :dependent => :destroy
has_many :subcategories, :through => :categories
and then the index should recognize the association, though the docs point out that you need an explicit alias when doing this, so: 然后索引应该识别该关联,尽管文档指出您在执行此操作时需要显式别名,因此:
indexes subcategories.subcategory_name, :as => 'subcategory_name'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.