Rails 4 关系#全部弃用 [英] Rails 4 Relation#all deprecation

查看:40
本文介绍了Rails 4 关系#全部弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我创建了一个最近的帖子功能.

In my app I created a recent posts feature.

 @recentposts = Post.all(:order => 'created_at DESC', :limit => 5)

这个变量有点麻烦.当我运行测试时,出现以下错误:

This variable makes some trouble. When I run tests I have the following error:

弃用警告:Relation#all 已弃用.如果你想预先加载一个关系,你可以调用#load(例如Post.where(published: true).load).如果您想从关系中获取一组记录,您可以调用 #to_a(例如 Post.where(published: true).to_a).(从/home/mateusz/rails4/Bloggers/app/controllers/users_controller.rb:18 中的节目调用)

DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. Post.where(published: true).load). If you want to get an array of records from a relation, you can call #to_a (e.g. Post.where(published: true).to_a). (called from show at /home/mateusz/rails4/Bloggers/app/controllers/users_controller.rb:18)

我在 Google 上搜索解决方案,但没有找到...

I was seraching solution on Google but I don't find it...

推荐答案

只写:

@recentposts = Post.order('created_at DESC').limit(5)

to_a 不是必需的,因为数据是在需要时延迟加载的.

The to_a is not explicitly necessary, as the data is lazy loaded when needed.

这篇关于Rails 4 关系#全部弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆