コードは次のとおりです。
class Video < ActiveRecord::Base
delegate :name, :to => :video_type, :prefix => true, :allow_nil => true
belongs_to :athlete, :class_name => "Athlete"
end
class Athlete < User
has_many :videos
end
コンソール:
a = Athlete.last
a.videos.group_by(&:video_type_name)
VideoType Load (0.8ms) SELECT "video_types".* FROM "video_types" WHERE "video_types"."id" = 12 LIMIT 1
Called from: app/models/video.rb:9:in `video_type_name'
VideoType Load (0.9ms) SELECT "video_types".* FROM "video_types" WHERE "video_types"."id" = 14 LIMIT 1
Called from: app/models/video.rb:9:in `video_type_name'
VideoType Load (0.8ms) SELECT "video_types".* FROM "video_types" WHERE "video_types"."id" = 1 LIMIT 1
Called from: app/models/video.rb:9:in `video_type_name'
VideoType Load (0.7ms) SELECT "video_types".* FROM "video_types" WHERE "video_types"."id" = 9 LIMIT 1
Called from: app/models/video.rb:9:in `video_type_name'
VideoType Load (0.7ms) SELECT "video_types".* FROM "video_types" WHERE "video_types"."id" = 11 LIMIT 1
Called from: app/models/video.rb:9:in `video_type_name'
VideoType Load (0.8ms) SELECT "video_types".* FROM "video_types" WHERE "video_types"."id" = 2 LIMIT 1
Called from: app/models/video.rb:9:in `video_type_name'
VideoType Load (0.8ms) SELECT "video_types".* FROM "video_types" WHERE "video_types"."id" = 2 LIMIT 1
これを 1 回のデータベース呼び出しに減らす方法はありますか?