ActiveRecord和ActiveRecord :: Relation对象之间的区别 [英] Difference between ActiveRecord and ActiveRecord::Relation objects
问题描述
我已经搜索了但找不到ActiveRecord和ActiveRecord :: relation对象之间的区别的简要说明。
I have searched but not able to find the brief explanation for the difference between ActiveRecord and ActiveRecord::relation object.
我了解ActiveRecord是单个对象通过类似的方式查找
I have understand that ActiveRecord is the single object find by something like
User.find(1)
和ActiveRecord :: Relation是类似对象的数组,查找对象的方式类似于
And ActiveRecord::Relation is the array like object Find by something like
User.where(id: 1)
我正在寻找查询之间的区别执行或对其进行深入的解释,因此将清除其背后的整个概念。
I am looking for the difference between them in terms of query execution or deep explanation about them, so it will clear the whole concept behind it.
预先感谢!
推荐答案
ActiveRecord :: Base
的实例是一个对象,代表数据库的特定行(或可能保存到数据库中) )。
An instance of ActiveRecord::Base
is an object that represents a specific row of your database (or might be saved into the database).
而 ActiveRecord :: Relation
的实例表示可以针对您的查询运行的查询数据库(但不是运行)。通过调用 to_a
,每个每个
,第一个
等。在该 Relation
实例中,将返回一个实例或 ActiveRecord :: Base
实例的数组。
Whereas an instance of ActiveRecord::Relation
is a representation of a query that can be run against your database (but wasn't run yet). Once you run that query by calling to_a
, each
, first
etc. on that Relation
a single instance or an array of ActiveRecord::Base
instances will be returned.
这篇关于ActiveRecord和ActiveRecord :: Relation对象之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!