我有一个带有几个模型的简单的algolia设置,每个模型都有一些属性,例如:
class User < ActiveRecord::Base
include AlgoliaSearch
algoliasearch do
attributes :id, :name
add_attribute :_tags
end
def _tags
users = []
self.connections.each do |connection|
users.push('user_' + connection.user_id.to_s)
end
users
end
end
上面的设置很容易让我限制每个用户的搜索结果,以便用户仅查看他们有权查看的记录,以查看它们是否在Rails应用程序中关联/相关。
但是,在Connection
创建或销毁User
模型时,该模型不知道,因此会对搜索结果产生负面影响。
如何在Connection
模型中添加ActiveRecord回调并相应地更新_tags
记录上的键,以便将其_tags
更新为包含或排除(基于创建或销毁)?
tldr:_tags
在Algolia中的记录上具有['user_1', 'user_2']
由Rails中的关联确定的值。Algolia中的记录与user_2
Rails中的数据库之间的关联已删除,如何user_2
从Algolia中的记录上的标签数组中删除?
GitHub上有一个尚待解决的问题:检测基础关联中的更改。
同时,您可以在User
对象的after_save回调中强制对象的索引编制Connection
:
class User
algoliasearch do
# [...]
end
end
class Connection
after_save :reindex_user
belongs_to :user
private
def reindex_user
self.user.reindex!
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句