Skip to content

Commit ba66899

Browse files
mirjharrjulianguyen
authored andcommitted
implementing eager load on set_association_variables!
1 parent 6281c3a commit ba66899

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

app/controllers/moments_controller.rb

+16-5
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,23 @@ def moment_params
107107
end
108108

109109
def set_association_variables!
110-
@categories = current_user.categories.is_visible
111-
.or(Category.where(id: @moment.category_ids))
112-
.order(created_at: :desc)
110+
visible_categories = current_user.categories.is_visible
111+
extra_categories = Category.where(id: @moment.category_ids)
112+
113+
@categories = Category.where(id: visible_categories.select(:id))
114+
.or(extra_categories)
115+
.includes(:moments_categories)
116+
.order(created_at: :desc)
117+
113118
@category = Category.new
114-
@moods = current_user.moods.is_visible.or(Mood.where(id: @moment.mood_ids))
115-
.order(created_at: :desc)
119+
visible_moods = current_user.moods.is_visible
120+
extra_moods = Mood.where(id: @moment.mood_ids)
121+
122+
@moods = Mood.where(id: visible_moods.select(:id))
123+
.or(extra_moods)
124+
.includes(:moments_moods)
125+
.order(created_at: :desc)
126+
116127
@mood = Mood.new
117128
@strategies = associated_strategies
118129
@strategy = Strategy.new

0 commit comments

Comments
 (0)