在类数组中查找类对象时出现问题
我正在使用 Ruby on Rails 3.0.7,我想了解如何处理以下代码以便检索具有指定 id
的类对象。
在我的视图文件中,我有:
@records = Users.all # This returns an array (class)
在另一个文件中,部分模板,我想检索,例如,id
1的用户,但如果我这样做:
@records.find(1)
我得到一个枚举器所有记录的
(类):
<Enumerator: [<Users id: 1, ... ] >
如何找到 id
1(或其他 id)“a là Ruby on Rails Way”的用户?
更新
我使用@records = Users.all
在视图文件中,因为我的目标是最小化对数据库的调用,因为我需要迭代几乎所有记录并检查它们是否存在。例如,如果我这样做:
some_hash.each { |key, value|
put User.find(value)
}
并且我进入日志文件,我会看到很多数据库请求。
I am using Ruby on Rails 3.0.7 and I would like to understand how to handle the following code in order to retrieve a class objects with a specified id
.
In my view file I have:
@records = Users.all # This returns an array (class)
In another file, a partial template, I would like to retrieve, for example, the user with id
1, but if I make this:
@records.find(1)
I get an enumerator
(class) of all records:
<Enumerator: [<Users id: 1, ... ] >
How can I find the user with id
1 (or other ids) "a là Ruby on Rails Way"?
UPDATE
I use @records = Users.all
in a view file because I aim to minimize calls to the database since I need to iterate almost over all records and check them existence. If I do for example:
some_hash.each { |key, value|
put User.find(value)
}
and I go in the log file, I will see a lot of database requests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管这可能相当慢,而且我怀疑您正在开发的应用程序中存在一些不太理想的设计(不是判断,我们都经历过),Array#index 似乎是您正在寻找的内容:
编辑
尽管如果您需要为每个用户做点什么,你需要为了通过 id 快速访问它们,我可能会在控制器中执行类似的操作。即使它不是真的更快,它也更具可读性(无论如何对我来说):
然后在你看来你可以这样做:
Even though this is probably quite slow, and I suspect there are some less than optimal designs in the app you're working on (not judging, we've all been there), Array#index seems to be what you're looking for:
Edit
Although if you need to do something for every user, and you need to access them by id quickly, I'd probably do something like this in your controller. Even if it's not really faster, it's much more readable (to me anyways):
Then in your views you can do:
使用
User.scoped
而不是User.all
。#all
将立即查询数据库并返回一个数组,而#scoped
将返回一个ActiveRecord::Relation
您可以链接进一步查询的对象。在这种情况下,除非您尝试以某种方式检查或枚举结果,否则数据库不会被命中Use
User.scoped
instead ofUser.all
.#all
will immediately query the database and return an array, whereas#scoped
will return anActiveRecord::Relation
object which you can chain further queries. In this case, the database won't be hit until you try and somehow inspect or enumerate the result其实你错了。
@records.find(1)
返回 Enumerator 类的对象(与 Enumerator 类本身不同)。这里的问题是,正如您所指出的,
@records
是一个数组,而不是 ActiveRecord 对象,并且Array#find
(继承自Enumerable#find
- 当未给出时一个块,返回 Enumerable 类的对象)与 ActiveRecord::Base#find 不同(即 User#find)。您应该做的是,在控制器中选择您想要的一个用户记录:
...然后直接在模板中使用
@user
。一般来说,您应该避免在模板中进行 ActiveRecord 查找(例如find
)。这种逻辑应该发生在你的控制器中。Actually you're mistaken.
@records.find(1)
is returning an object of the class Enumerator (which is not the same as the class Enumerator itself).The problem here is that, as you've noted,
@records
is an Array, not an ActiveRecord object, andArray#find
(inherited fromEnumerable#find
--which, when not given a block, returns an object of class Enumerable) is not the same method asActiveRecord::Base#find
(i.e.User#find
).What you should do is, in your controller, pick out the one user record you want:
...and then use
@user
directly in your template. Generally you should avoid doing ActiveRecord lookups (e.g.find
) in your templates. That kind of logic should happen in your controller.上次对于这种情况我最终这样做了:
Last time for such case I ended up doing like this: