Reputation: 964
I have a simple algolia setup with a few models and each model has a few attributes, ex:
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
The setup above easily allows me to restrict search results on a per user basis so that the user only sees the records that they have permission to see if they are associated/related in the Rails app.
However, when the Connection
model is created or destroyed, the User
model is unaware and thus affects the search results negatively.
How can I add an ActiveRecord callback to the Connection
model and update the _tags
key on the record accordingly so that the _tags
is updated to include or exclude (based on create or destroy)?
tldr: _tags
on a record in Algolia has the value ['user_1', 'user_2']
determined by associations in Rails. The association between the record in Algolia and user_2
is removed from the DB in Rails, how can I remove user_2
from the array of tags on the record in Algolia?
Upvotes: 1
Views: 372
Reputation: 2309
There is a pending issue on GitHub about that: detecting changes in the underlying associations.
In the meantime, you could force the indexing of the User
object in an after_save callback of the Connection
object:
class User
algoliasearch do
# [...]
end
end
class Connection
after_save :reindex_user
belongs_to :user
private
def reindex_user
self.user.reindex!
end
end
Upvotes: 2