How to preload Rails scopes

Rails’ scopes make it easy to find the records you want:

app/models/review.rb1 2 3 4 5 class Review < ActiveRecord::Base belongs_to :restaurant scope :positive, - { where("rating 3.0") } end 1 2 3 4 irb(main):001:0 Restaurant.first.reviews.positive.count Restaurant Load (0.4ms) SELECT `restaurants`.* FROM `restaurants` ORDER BY `restaurants`.`id` ASC LIMIT 1 (0.6ms) SELECT COUNT(*) FROM `reviews` WHERE `reviews`.`restaurant_id` = 1 AND (rating 3.0) = 5

But if you’re not careful with t...

<
 •  0 comments  •  flag
Share on Twitter
Published on June 24, 2015 08:57
No comments have been added yet.