【问题标题】:Mapping search results in rails when working with Association使用 Association 时在 Rails 中映射搜索结果
【发布时间】:2013-01-29 12:17:25
【问题描述】:

我有两个模型:

Product { has_may variants}


Variant {belongs to product}

我使用了 Sunspot 搜索,结果我检索到了选定的变体,现在我想要这个结果,因为我的搜索结果包含搜索检索到的一个或多个变体的每个产品。

搜索代码示例如下:

@search = Sunspot.search(Spree::Variant) do
  keywords params[:keywords]      
  with :is_active, true  
  with  :deleted_at,nil
  if params[:ah].present? && params[:al].present?
    (Date.parse(params[:al])..Date.parse(params[:ah])).each do |d|
    with :f2r_available_on, d.to_time
    end
  end
end
@products = @search.results

【问题讨论】:

    标签: ruby-on-rails-3 search mapping sunspot-rails


    【解决方案1】:

    首先让我澄清一下:在您上面的代码中,@search.results 是从您的搜索返回的变体记录数组,您想要获取与这些变体中的一个或多个相关联的所有产品吗?

    如果是这样,那么应该这样做:

    @products = Product.where(:id => @search.results.map(&:product_id))
    

    【讨论】:

    • 它给了我错误:未初始化的常量 Spree::ProductsController::Product
    • 您的product 型号叫什么?也许Spree::Product?如果是,那么:@products = Spree::Product.where(:id => @serach.results.map(&:product_id)).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多