Searchkick 2 嵌套 'has_many'

Searchkick 2 nesting 'has_many'

我有 3 个 类。

Product, Command, CommandOption

我有搜索效果很好。按产品搜索。 但是我想通过 CommandOption 搜索并返回我的 Product 对象,这是怎么做到的?

class Product < ActiveRecord::Base


extend FriendlyId
  friendly_id :slug, use: :slugged



searchkick

  has_many :commands

  def search_data
    {
      name: name,

      #commands (has_many)
      command_captions: commands.map(&:caption).join(' '),
      command_numbers: commands.map(&:number).join(' '),

      #if I write here, then get error
      #command_option_caption: command_options.map(&:caption).join('')
    }
  end

end

class Command < ActiveRecord::Base
  belongs_to :product

  has_many :command_options
end

class CommandOption < ActiveRecord::Base
  belongs_to :command
end

对不起我的英语

我找到了解决方案!

只是在模型中 Product 添加关系

class Product < ActiveRecord::Base
  searchkick

  has_many :commands
  has_many :command_options, through: :commands


  def search_data
    {
      name: name,

      #commands (has_many)
      command_captions: commands.map(&:caption).join(' '),
      command_numbers: commands.map(&:number).join(' '),
      #for commandoption model
      command_option_captions: command_options.map(&:caption).join(' ')
    }
  end

end