【问题标题】:Ruby on Rails thinking-sphinx has many through indexingRuby on Rails thinking-sphinx 有很多通过索引
【发布时间】:2023-12-28 21:20:02
【问题描述】:

干杯!我有tour 具有这些关联的模型:

  has_many :tour_in_the_countries
  has_many :country, :through => :tour_in_the_countries

在 tour_index.rb 中

ThinkingSphinx::Index.define :tour, :with => :active_record do
  indexes :title
  indexes :preview
end

在这种情况下如何添加到索引国家的名称?

【问题讨论】:

    标签: ruby-on-rails full-text-search thinking-sphinx


    【解决方案1】:

    这应该可以解决问题:

    indexes tour_in_the_countries.country.name, :as => :countries
    

    【讨论】:

    • 另外:我个人会将has_many :country ... 重命名为has_many :countries ... - Rails 足够聪明,可以识别您是指 TourInTheCountry 上的 country 关联
    • 对不起,我建立了索引,虽然一切正常,但是当我尝试搜索时:Tour.search :conditions => {:countries => 'Australia'} 它返回了undefined method conditions= for #<MetaSearch::Searches::Tour:0x007f98c125bb90> 的 no_method 错误
    • 看起来您的 Gemfile 中同时拥有 Thinking Sphinx 和 MetaSearch - 您需要两者吗?如果是这样,我认为在您的 Gemfile 中的 meta_search 之后放置 thinking-sphinx 应该确保它们以正确的顺序加载。
    • 确实,我正在使用正在使用元搜索的 active-admin gem!
    • 只要确保 activeadmin 在您的 Gemfile 中位于 thinking-sphinx 之前,它应该可以正常工作(我有几个项目同时使用这两者)。