2016-05-02 13 views
0

私は3つのクラスを持っています。Searchkick 2ネスト 'has_many'

Product, Command, CommandOption 

私は非常に良い仕事を検索しています。製品別に検索してください。 しかし、私はCommandOptionで検索し、私のProductオブジェクトを返しました。これはどうですか?私の英語

答えて

0

のため申し訳ありません

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 
に単に

関連する問題