This article is also available in Korean, thanks to Soonsang Hong!
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`.`restaur...<
Published on June 23, 2015 13:17