为什么控制台中没有返回 Active Record 关系?
我终于开始将我的 Rails 应用程序从 2.3.8 升级到 3.1.0。我正在观看 RailsCasts (http://railscasts.com/episodes/ 202-active-record-queries-in-rails-3)有关 Active Record 查询。
当我打开控制台 (rails c) 并执行与此类似的查询时:
articles = Article.order("name")
我看到执行的查询,而不是返回 Active Record 关系。我在这里做错了什么?
Rails 版本:
1.9.2 上的 3.1.0 RVM
感谢您的帮助!
编辑:我添加了示例的屏幕截图。
I have finally started upgrading my Rails apps from 2.3.8 to 3.1.0. I was watching RailsCasts (http://railscasts.com/episodes/202-active-record-queries-in-rails-3) about Active Record queries.
When I open up the console (rails c) and do query similar to this:
articles = Article.order("name")
Instead of returning Active Record relations, I see the query executed. What am I doing wrong here?
Rails version: 3.1.0
RVM on 1.9.2
Thank you for your help!
EDIT: I have added a screenshot from the example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ActiveRecord Relation 类配置为在调用
.all
、.last
、.first
、... 等查询方法时执行查询。方法列表还包括.inspect
,控制台调用该方法来显示返回值的表示形式。因此,在您看来,该对象永远不是关系,因为您总是看到查询的结果。
但是如果你检查对象类,你会发现它是一个关系
The ActiveRecord Relation class is configured to perform the query when a query method like
.all
,.last
,.first
, ... is invoked. The list of method also includes.inspect
, the same method called by the console to display the representation of the return value.For this reason it seems to you that the object is never a relation, because you always see the result of the query.
But if you inspect the object class, you'll notice it's a relation
你做的一切都是对的。您会看到执行的查询是因为控制台在输出上调用
inspect
方法。尝试articles = Article.order("name").class
You're doing everything right. You see query executed because console invokes
inspect
method on output. Tryarticles = Article.order("name").class