本文介绍了轨道范围3的ActiveRecord立即加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我。我有一些模式,HAS_MANY与其他模型关联。例如:配置=>的has_many:统计和统计模型中,我有一些范围:
Help me please.I have some model which has_many association with other model.For example: profile => has_many :statisticsAnd inside of statistic model I have some scope:
scope last_ten, limit(10).order('online desc')
和问题是我怎么可以用贪婪加载这个范围是什么?我不需要统计分布的每个记录。只有作用域。
And question is how can I use eager load for this scope? I don't need every record of statistics for profile. Only scoped.
现在我只能用
User.profiles.includes(:statistics)
感谢。
推荐答案
按如下说明:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
这是更好地定义自定义关系:
It's better to define a custom relation:
class Profile < ActiveRecord::Base
has_many :most_recent_stats, :class_name => 'Statistic', :order => 'online DESC', :limit => 10
...
end
User.profiles.includes(:most_recent_stats)
这篇关于轨道范围3的ActiveRecord立即加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!