ruby-on-rails - solr sunspot - 搜索belongs_to 关联

标签 ruby-on-rails ruby-on-rails-3 solr full-text-search sunspot

我有一个属于多个其他表的织物模型。

class Fabric < ActiveRecord::Base
  validates :name, presence: true
  belongs_to :design
  belongs_to :composition
  belongs_to :collection
  belongs_to :style
  belongs_to :origin
  belongs_to :texture
  belongs_to :supplier
  has_and_belongs_to_many :colours

  searchable do
    text :name, :boost => 5 
    text :description
    text :composition do
      composition.name
   end
    text :collection do
      collection.name
    end
   text :style do
     style.name
   end
   text :origin do
     origin.name
   end
   text :texture do
     texture.name
  end
   text :supplier do
      supplier.name
  end
  end
  end

我已经设置了所有反向关联(Has_many)等。
但是,我似乎无法通过全文搜索来查询所有这些关联表的名称字段。

任何帮助将不胜感激。
 @search = Fabric.search do
    fulltext params[:search]
  end
  @fabrics = @search.results

罗斯

最佳答案

如果某些关联可能为零,请不要忘记对此进行测试,否则在重建索引时会出错

text :collection do 
  collection.name if collection
end

关于ruby-on-rails - solr sunspot - 搜索belongs_to 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10768267/

相关文章:

ruby-on-rails - 我怎样才能以一种很好的方式呈现 postgres 错误?

ruby-on-rails - Rails 连接查询

ruby-on-rails - Rails 设计 : user_signed_in? 不工作

ruby-on-rails - NoMethodError : undefined method `spec' for nil:NilClass - active_utils, factory_girl 和更多 gems 失败

ruby-on-rails - 使用 Rails 3 link_to 执行 ajax 回调的正确方法是什么

javascript - 使用 SublimeLinter-jshint 忽略 javascript 中的 ERB

ruby-on-rails-3 - 每当 gem 不运行时的 cron

php - solr 的 ping 多核

solr - 如何在 Lucene(Solr) 中合并段

apache - Solr 拼写检查与模糊搜索