Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove n+1 talks controller with Meilisearch #56

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/talks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
@from_talk_id = session[:from_talk_id]
session[:from_talk_id] = nil
if params[:q].present?
talks = Talk.pagy_search(params[:q])
talks = Talk.includes(:speakers, :event).pagy_search(params[:q])
@pagy, @talks = pagy_meilisearch(talks, items: 9, page: session[:talks_page]&.to_i || 1)
else
@pagy, @talks = pagy(Talk.all.order(date: :desc).includes(:speakers, :event), items: 9, page: session[:talks_page]&.to_i || 1)
Expand Down
11 changes: 9 additions & 2 deletions app/models/talk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Talk < ApplicationRecord
include Suggestable
slug_from :title
include MeiliSearch::Rails
ActiveRecord_Relation.include Pagy::Meilisearch
extend Pagy::Meilisearch

# associations
Expand All @@ -51,17 +52,23 @@ class Talk < ApplicationRecord
attribute :thumbnail_sm
attribute :thumbnail_md
attribute :thumbnail_lg
attribute :speakers do
attribute :speaker_names do
speakers.pluck(:name)
end
searchable_attributes [:title, :description]
attribute :event_name do
event_name
end
searchable_attributes [:title, :description, :speaker_names, :event_name]
sortable_attributes [:title]

attributes_to_highlight ["*"]
end

meilisearch enqueue: true

# ensure that during the reindex process the associated records are eager loaded
scope :meilisearch_import, -> { includes(:speakers, :event) }

def to_meta_tags
{
title: title,
Expand Down
2 changes: 1 addition & 1 deletion app/views/talks/_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<% if talk.event %>
<div class="flex items-center gap-2">
<%= heroicon :map_pin, size: :sm, class: "shrink-0 grow-0" %>
<%= content_tag :div, talk.event.name %>
<%= content_tag :div, talk.event_name %>
</div>
<%#= link_to talk.event.name, talk.event %>
<% end %>
Expand Down