加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用太阳黑子重新编制一个特定记录

发布时间:2020-12-17 03:53:54 所属栏目:百科 来源:网络整理
导读:我有两个型号 class User ActiveRecord::Base has_many :posts searchable do text :post_titles end def post_titles posts.map :title endendclass Post ActiveRecord::Base belongs_to :userend 问题是,当我更新Post太阳黑子的标题时,不会更新相关用户的
我有两个型号

class User < ActiveRecord::Base
  has_many :posts

  searchable do
    text :post_titles
  end

  def post_titles
    posts.map &:title
  end
end

class Post < ActiveRecord::Base
  belongs_to :user
end

问题是,当我更新Post太阳黑子的标题时,不会更新相关用户的索引,也无法通过新数据进行搜索.如果我做User.index它解决了问题,但需要花费太多时间.是否有更好的解决方案来更新子记录更改的父记录索引(如reindex只是父记录而不是所有用户)?

解决方法

Sunspot提供了一个实例index()方法,用于索引一条记录.

我做的是

class Post 

   belongs_to :user
   after_save :update_user_index

 private

   def update_user_index
     user.index
   end
end

如果您在控制台中运行此命令,并希望立即查看结果,请致电Sunspot.commit

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读