在类数组中查找类对象时出现问题

发布于 2024-11-15 07:00:23 字数 733 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-11-22 07:00:23

尽管这可能相当慢,而且我怀疑您正在开发的应用程序中存在一些不太理想的设计(不是判断,我们都经历过),Array#index 似乎是您正在寻找的内容:

@records[@records.index{|user| user.id == 1}]

编辑

尽管如果您需要为每个用户做点什么,你需要为了通过 id 快速访问它们,我可能会在控制器中执行类似的操作。即使它不是真的更快,它也更具可读性(无论如何对我来说):

@users_hash = {}
User.all.each{|user| @users_hash[user.id] = user}

然后在你看来你可以这样做:

@users_hash[id].username

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:

@records[@records.index{|user| user.id == 1}]

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):

@users_hash = {}
User.all.each{|user| @users_hash[user.id] = user}

Then in your views you can do:

@users_hash[id].username
会傲 2024-11-22 07:00:23

使用 User.scoped 而不是 User.all#all立即查询数据库并返回一个数组,而#scoped 将返回一个 ActiveRecord::Relation您可以链接进一步查询的对象。在这种情况下,除非您尝试以某种方式检查或枚举结果,否则数据库不会被命中

Use User.scoped instead of User.all. #all will immediately query the database and return an array, whereas #scoped will return an ActiveRecord::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

囍孤女 2024-11-22 07:00:23

其实你错了。 @records.find(1) 返回 Enumerator 类的对象(与 Enumerator 类本身不同)。

这里的问题是,正如您所指出的,@records 是一个数组,而不是 ActiveRecord 对象,并且 Array#find (继承自 Enumerable#find - 当未给出时一个块,返回 Enumerable 类的对象)与 ActiveRecord::Base#find 不同(即 User#find)。

您应该做的是,在控制器中选择您想要的一个用户记录:

@user = User.find 1

...然后直接在模板中使用 @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, and Array#find (inherited from Enumerable#find--which, when not given a block, returns an object of class Enumerable) is not the same method as ActiveRecord::Base#find (i.e. User#find).

What you should do is, in your controller, pick out the one user record you want:

@user = User.find 1

...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.

烂人 2024-11-22 07:00:23

上次对于这种情况我最终这样做了:

@assignments = Assignment.find_by_sql(' ... ')

@assignments.find(id: 1).first

Last time for such case I ended up doing like this:

@assignments = Assignment.find_by_sql(' ... ')

@assignments.find(id: 1).first
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文