Query for all courses bookmarked by the current user.
This works, but it requires two queries.
@bookmarks = current_user.bookmarks
@courses = Course.where(id: @bookmarks.select(:bookmarkable_id))
This is more efficient because it results in one query thanks to the call to joins
.
@courses = Course.joins(:bookmarks).where("bookmarks.user_id = ?", current_user.id)
https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-where-label-Joins