Rails 3 ActiveRecord渴望加载范围
请帮帮我。 我有一些与其他模型有关联的模型。 例如:profile => has_many:statistics在统计模型中我有一些范围:
scope last_ten, limit(10).order('online desc')
问题是如何在此范围内使用预先加载? 我不需要每个配置文件的统计记录。 只有范围。
现在我只能使用
User.profiles.includes(:statistics)
谢谢。
如下所述: http : //api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
最好定义自定义关系:
class Profile < ActiveRecord::Base has_many :most_recent_stats, :class_name => 'Statistic', :order => 'online DESC', :limit => 10 ... end User.profiles.includes(:most_recent_stats)