bensheldon/activerecord-has_some_of_many: Optimized top-N-per-group Active Record associations using lateral joins
24-Jan-2025 140
This gem adds new optimized Active Record association methods (has_one_of_many, has_some_of_many) for "top N" queries to ActiveRecord using JOIN LATERAL that are eager-loadable (includes(:association), preloads(:association)) to avoid N+1 queries, and is compatible with typical queries and batch methods (find_each, in_batches, find_in_batches). For example, you might have these types of queries in your application:
Users have many posts, and you want to query the most recent post for each user
Posts have many comments, and you want to query the 5 most recent visible comments for each post
Posts have many comments, and you want to query the one comment with the largest votes_count for each post.
bensheldon/activerecord-has_some_of_many: Optimized top-N-per-group Active Record associations using lateral joins #ruby #rubydeveloper #rubyonrails #bensheldon/activerecord-has_some_of_many: #Optimized #top-N-per-group #Active #Record #associations #using #lateral #joins #active #record https://rubyonrails.ba/link/bensheldon-activerecord-has_some_of_many-optimized-top-n-per-group-active-record-associations-using-lateral-joins