【问题标题】:Rails 3 - how to search with Solr in associations?Rails 3 - 如何在关联中使用 Solr 进行搜索?
【发布时间】:2012-03-07 15:25:33
【问题描述】:

我已经一整天都在努力设置 Solr 以进行关联搜索。

我有一张 photos 表格,这张表格有 naturecity 之类的标签。这些标签保存在标签表中。 photo_tags 表中保存有 photo_idtag_id

当我尝试帮助 nature 这个词时,我想获得所有带有此标签的照片的声明,我尝试这样做(模型 photo ):

  searchable do
    text :photo_tags do
      photo_tags.map{ |photo_tag| photo_tags.tag.name }
    end
  end

但是我没有得到带有标签的照片...

编辑 - 我的关联:

photo.rb
  has_many :photo_tags
  has_many :tags, :through => :photo_tags

photo_tag.rb
  belongs_to :photo
  belongs_to :tag

【问题讨论】:

    标签: ruby-on-rails-3 solr model-associations sunspot-solr


    【解决方案1】:

    为什么在地图中使用photo_tags 而不是photo_tag

    也许这应该可以解决问题:

    searchable do
      text :photo_tags do
        photo_tags.map{ |photo_tag| photo_tag.name }
      end
    end
    

    【讨论】:

    • 不幸的是,这对我不起作用,我添加到我原来的问题关联中
    • 您要使用的关联名称似乎是tags 而不是photo_tags所以您可以尝试tags.map{...} 而不是photo_tags.map{...}
    • 很确定他是对的,使用tags.map(&:name) 应该没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多