0

我有两个模型,产品和商店通过库存模型关联在一起。根据商店位置搜索商店产品

class Product < ApplicationRecord 
    searchkick 
    has_many :stocks 
    has_many :stores, through: :stocks 
end 

class Store < ApplicationRecord 
    searchkick locations: [:location] 
    belongs_to :user 
    has_many :stocks 
    has_many :products, through: :stocks 
end 

使用Searchkick(弹性)或ActiveRecord的我要搜索的产品,并获得匹配的产品的查询,包括它有他们的商店,这些商店的距离排序(中订单部分不是很重要)。

有谁知道我该如何做到这一点?

回答

0

最简单的方法使用ActiveRecord是获取所有产品匹配的搜索和明年的查询检索具有这些同类产品商店:

products = Product.where(your_search_params) 
stores = Stores.joins(:stocks).where('stocks.product_id IN (?)', products.ids)