Sunspot / Solr / Rails: Model Associations are not updating in the Index

隐身守侯 提交于 2019-12-03 08:18:29

You're right that :auto_index and :auto_remove don't apply to associated objects, just the searchable object they are specified on.

When denormalizing, you should use after_save hooks on the associated objects to trigger a reindex where necessary. In this case, you want changes to the Activity model and the FieldnoteActivity join model to trigger a reindex of their associated Fieldnote objects when saved or destroyed.

class Fieldnote
  has_many :fieldnote_activities
  has_many :activities, :through => :fieldnote_activities

  searchable do
    # index denormalized data from activities
  end
end

class FieldnoteActivity
  has_many :fieldnotes
  has_many :activities

  after_save :reindex_fieldnotes
  before_destroy :reindex_fieldnotes

  def reindex_fieldnotes
    Sunspot.index(fieldnotes)
  end
end

class Activity
  has_many :fieldnote_activities
  has_many :fieldnotes, :through => :fieldnote_activities

  after_save :reindex_fieldnotes
  before_destroy :reindex_fieldnotes

  def reindex_fieldnotes
    Sunspot.index(fieldnotes)
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!