Ruby on rails Rails:Enumerable#group_by with Delegation进行了太多的DB调用
代码如下:Ruby on rails Rails:Enumerable#group_by with Delegation进行了太多的DB调用,ruby-on-rails,activerecord,group-by,Ruby On Rails,Activerecord,Group By,代码如下: 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 有没有办法将此减少为一次数据库调用?尝试使
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
有没有办法将此减少为一次数据库调用?尝试使用
包含或加入。这将创建一个SQL
查询,根据您的喜好创建表和组之间的连接
a.includes(:videos => :video_types).group('video_types.video_type_name')
视频
是否属于:视频类型
?尝试使用a.videos。包括(:video\u type)。分组依据(&:video\u type\u name)
a.includes(:videos => :video_types).group('video_types.video_type_name')