加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3 ActiveRecord加载范围

发布时间:2020-12-16 20:05:35 所属栏目:百科 来源:网络整理
导读:请帮帮我. 我有一些模型has_many与其他模型相关联. 例如:profile = has_many:statistics 在统计模型里面我有一些范围: scope last_ten,limit(10).order('online desc') 问题是如何在这个范围内使用热切的负载?我不需要统计资料的每一个纪录.只有范围. 现
请帮帮我.
我有一些模型has_many与其他模型相关联.
例如: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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读